2019-09-18 08:02:36 -07:00
|
|
|
/* Copyright (C) 2019 Wildfire Games.
|
2009-06-20 09:13:29 -07:00
|
|
|
* This file is part of 0 A.D.
|
|
|
|
|
*
|
|
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2006-04-23 16:14:18 -07:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
|
|
#include "MessageHandler.h"
|
|
|
|
|
#include "../MessagePasserImpl.h"
|
|
|
|
|
|
2010-06-01 17:36:21 -07:00
|
|
|
#include "../GameLoop.h"
|
|
|
|
|
#include "../View.h"
|
2015-12-30 13:16:35 -08:00
|
|
|
#include "graphics/CinemaManager.h"
|
2010-06-01 17:36:21 -07:00
|
|
|
#include "graphics/GameView.h"
|
|
|
|
|
#include "gui/GUIManager.h"
|
2019-09-18 13:51:45 -07:00
|
|
|
#include "gui/CGUI.h"
|
2012-01-12 15:32:27 -08:00
|
|
|
#include "lib/external_libraries/libsdl.h"
|
2019-09-20 06:11:18 -07:00
|
|
|
#include "lib/ogl.h"
|
2010-01-09 11:20:14 -08:00
|
|
|
#include "lib/sysdep/cpu.h"
|
2010-06-01 17:36:21 -07:00
|
|
|
#include "maths/MathUtil.h"
|
|
|
|
|
#include "ps/Game.h"
|
|
|
|
|
#include "ps/Util.h"
|
|
|
|
|
#include "ps/GameSetup/Config.h"
|
2006-12-04 18:35:00 -08:00
|
|
|
#include "ps/GameSetup/GameSetup.h"
|
2010-06-01 17:36:21 -07:00
|
|
|
#include "renderer/Renderer.h"
|
2014-09-12 14:38:10 -07:00
|
|
|
#include "simulation2/Simulation2.h"
|
|
|
|
|
#include "simulation2/components/ICmpSoundManager.h"
|
2006-12-04 18:35:00 -08:00
|
|
|
|
|
|
|
|
extern void (*Atlas_GLSwapBuffers)(void* context);
|
|
|
|
|
|
2006-04-23 16:14:18 -07:00
|
|
|
namespace AtlasMessage {
|
|
|
|
|
|
|
|
|
|
MESSAGEHANDLER(MessageTrace)
|
|
|
|
|
{
|
|
|
|
|
((MessagePasserImpl*)g_MessagePasser)->SetTrace(msg->enable);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MESSAGEHANDLER(Screenshot)
|
|
|
|
|
{
|
2012-03-05 16:15:48 -08:00
|
|
|
if (msg->big)
|
|
|
|
|
WriteBigScreenshot(L".bmp", msg->tiles);
|
|
|
|
|
else
|
|
|
|
|
WriteScreenshot(L".png");
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
|
2006-12-04 18:35:00 -08:00
|
|
|
QUERYHANDLER(CinemaRecord)
|
|
|
|
|
{
|
2007-02-09 09:04:55 -08:00
|
|
|
const int w = msg->width, h = msg->height;
|
2007-02-01 18:17:15 -08:00
|
|
|
|
2006-12-04 18:35:00 -08:00
|
|
|
{
|
|
|
|
|
g_Renderer.Resize(w, h);
|
|
|
|
|
SViewPort vp = { 0, 0, w, h };
|
2019-10-03 05:58:47 -07:00
|
|
|
g_Game->GetView()->SetViewport(vp);
|
2006-12-04 18:35:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsigned char* img = new unsigned char [w*h*3];
|
2007-02-09 09:04:55 -08:00
|
|
|
unsigned char* temp = new unsigned char[w*3];
|
2006-12-04 18:35:00 -08:00
|
|
|
|
|
|
|
|
int num_frames = msg->framerate * msg->duration;
|
|
|
|
|
|
2012-03-03 15:48:04 -08:00
|
|
|
AtlasView::GetView_Game()->SaveState(L"cinema_record");
|
2007-01-16 19:25:20 -08:00
|
|
|
|
|
|
|
|
// Set it to update the simulation at normal speed
|
2012-03-03 15:48:04 -08:00
|
|
|
AtlasView::GetView_Game()->SetSpeedMultiplier(1.f);
|
2007-01-16 19:25:20 -08:00
|
|
|
|
2006-12-04 18:35:00 -08:00
|
|
|
for (int frame = 0; frame < num_frames; ++frame)
|
|
|
|
|
{
|
2012-03-03 15:48:04 -08:00
|
|
|
AtlasView::GetView_Game()->Update(1.f / msg->framerate);
|
2007-01-16 19:25:20 -08:00
|
|
|
|
2006-12-04 18:35:00 -08:00
|
|
|
Render();
|
2012-03-03 15:48:04 -08:00
|
|
|
Atlas_GLSwapBuffers((void*)g_AtlasGameLoop->glCanvas);
|
2007-01-16 19:25:20 -08:00
|
|
|
|
2006-12-04 18:35:00 -08:00
|
|
|
glReadPixels(0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, img);
|
|
|
|
|
|
|
|
|
|
// Swap the rows around, else the image will be upside down
|
2007-02-09 09:04:55 -08:00
|
|
|
//* // TODO: BGR24 output doesn't need flipping, YUV420 and RGBA32 do
|
2006-12-04 18:35:00 -08:00
|
|
|
for (int y = 0; y < h/2; ++y)
|
|
|
|
|
{
|
2010-11-01 04:09:03 -07:00
|
|
|
memcpy(temp, &img[y*w*3], w*3);
|
|
|
|
|
memcpy(&img[y*w*3], &img[(h-1-y)*w*3], w*3);
|
|
|
|
|
memcpy(&img[(h-1-y)*w*3], temp, w*3);
|
2006-12-04 18:35:00 -08:00
|
|
|
}
|
2007-02-09 09:04:55 -08:00
|
|
|
//*/
|
2006-12-04 18:35:00 -08:00
|
|
|
|
|
|
|
|
// Call the user-supplied function with this data, so they can
|
|
|
|
|
// store it as a video
|
|
|
|
|
sCinemaRecordCB cbdata = { img };
|
|
|
|
|
msg->cb.Call(cbdata);
|
|
|
|
|
}
|
|
|
|
|
|
2007-01-16 19:25:20 -08:00
|
|
|
// Pause the game once we've finished
|
2012-03-03 15:48:04 -08:00
|
|
|
AtlasView::GetView_Game()->SetSpeedMultiplier(0.f);
|
2007-01-16 19:25:20 -08:00
|
|
|
|
2012-03-03 15:48:04 -08:00
|
|
|
AtlasView::GetView_Game()->RestoreState(L"cinema_record");
|
2007-01-16 19:25:20 -08:00
|
|
|
// TODO: delete the saved state now that we don't need it any more
|
|
|
|
|
|
2006-12-04 18:35:00 -08:00
|
|
|
delete[] img;
|
2007-02-09 09:04:55 -08:00
|
|
|
delete[] temp;
|
2006-12-04 18:35:00 -08:00
|
|
|
|
|
|
|
|
// Restore viewport
|
|
|
|
|
{
|
|
|
|
|
g_Renderer.Resize(g_xres, g_yres);
|
|
|
|
|
SViewPort vp = { 0, 0, g_xres, g_yres };
|
2019-10-03 05:58:47 -07:00
|
|
|
g_Game->GetView()->SetViewport(vp);
|
2006-12-04 18:35:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2006-07-12 07:49:10 -07:00
|
|
|
QUERYHANDLER(Ping)
|
|
|
|
|
{
|
|
|
|
|
UNUSED2(msg);
|
|
|
|
|
}
|
2016-11-23 05:02:58 -08:00
|
|
|
|
2014-12-29 13:01:54 -08:00
|
|
|
MESSAGEHANDLER(SimStopMusic)
|
|
|
|
|
{
|
2014-12-30 09:27:18 -08:00
|
|
|
UNUSED2(msg);
|
|
|
|
|
|
|
|
|
|
CmpPtr<ICmpSoundManager> cmpSoundManager(*g_Game->GetSimulation2(), SYSTEM_ENTITY);
|
|
|
|
|
if (cmpSoundManager)
|
|
|
|
|
cmpSoundManager->StopMusic();
|
2014-12-29 13:01:54 -08:00
|
|
|
}
|
2006-04-23 16:14:18 -07:00
|
|
|
|
2007-01-16 19:25:20 -08:00
|
|
|
MESSAGEHANDLER(SimStateSave)
|
|
|
|
|
{
|
2012-03-03 15:48:04 -08:00
|
|
|
AtlasView::GetView_Game()->SaveState(*msg->label);
|
2007-01-16 19:25:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MESSAGEHANDLER(SimStateRestore)
|
|
|
|
|
{
|
2012-03-03 15:48:04 -08:00
|
|
|
AtlasView::GetView_Game()->RestoreState(*msg->label);
|
2007-01-16 19:25:20 -08:00
|
|
|
}
|
|
|
|
|
|
2010-01-09 11:20:14 -08:00
|
|
|
QUERYHANDLER(SimStateDebugDump)
|
|
|
|
|
{
|
2012-03-03 15:48:04 -08:00
|
|
|
msg->dump = AtlasView::GetView_Game()->DumpState(msg->binary);
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
|
2007-01-16 19:25:20 -08:00
|
|
|
MESSAGEHANDLER(SimPlay)
|
|
|
|
|
{
|
2012-03-03 15:48:04 -08:00
|
|
|
AtlasView::GetView_Game()->SetSpeedMultiplier(msg->speed);
|
2013-02-22 15:20:59 -08:00
|
|
|
AtlasView::GetView_Game()->SetTesting(msg->simTest);
|
2007-01-16 19:25:20 -08:00
|
|
|
}
|
|
|
|
|
|
2007-02-01 18:17:15 -08:00
|
|
|
MESSAGEHANDLER(JavaScript)
|
|
|
|
|
{
|
2014-01-04 02:14:53 -08:00
|
|
|
g_GUI->GetActiveGUI()->GetScriptInterface()->LoadGlobalScript(L"Atlas", *msg->command);
|
2007-02-01 18:17:15 -08:00
|
|
|
}
|
|
|
|
|
|
2010-06-01 17:36:21 -07:00
|
|
|
MESSAGEHANDLER(GuiSwitchPage)
|
|
|
|
|
{
|
2014-08-04 13:14:17 -07:00
|
|
|
g_GUI->SwitchPage(*msg->page, NULL, JS::UndefinedHandleValue);
|
2010-06-01 17:36:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MESSAGEHANDLER(GuiMouseButtonEvent)
|
|
|
|
|
{
|
|
|
|
|
SDL_Event_ ev = { { 0 } };
|
|
|
|
|
ev.ev.type = msg->pressed ? SDL_MOUSEBUTTONDOWN : SDL_MOUSEBUTTONUP;
|
|
|
|
|
ev.ev.button.button = msg->button;
|
|
|
|
|
ev.ev.button.state = msg->pressed ? SDL_PRESSED : SDL_RELEASED;
|
2017-05-15 17:09:16 -07:00
|
|
|
ev.ev.button.clicks = msg->clicks;
|
2010-06-01 17:36:21 -07:00
|
|
|
float x, y;
|
|
|
|
|
msg->pos->GetScreenSpace(x, y);
|
2019-09-18 08:02:36 -07:00
|
|
|
ev.ev.button.x = static_cast<u16>(Clamp<int>(x, 0, g_xres));
|
|
|
|
|
ev.ev.button.y = static_cast<u16>(Clamp<int>(y, 0, g_yres));
|
2010-06-01 17:36:21 -07:00
|
|
|
in_dispatch_event(&ev);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MESSAGEHANDLER(GuiMouseMotionEvent)
|
|
|
|
|
{
|
|
|
|
|
SDL_Event_ ev = { { 0 } };
|
|
|
|
|
ev.ev.type = SDL_MOUSEMOTION;
|
|
|
|
|
float x, y;
|
|
|
|
|
msg->pos->GetScreenSpace(x, y);
|
2019-09-18 08:02:36 -07:00
|
|
|
ev.ev.motion.x = static_cast<u16>(Clamp<int>(x, 0, g_xres));
|
|
|
|
|
ev.ev.motion.y = static_cast<u16>(Clamp<int>(y, 0, g_yres));
|
2010-06-01 17:36:21 -07:00
|
|
|
in_dispatch_event(&ev);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MESSAGEHANDLER(GuiKeyEvent)
|
|
|
|
|
{
|
|
|
|
|
SDL_Event_ ev = { { 0 } };
|
|
|
|
|
ev.ev.type = msg->pressed ? SDL_KEYDOWN : SDL_KEYUP;
|
2012-02-06 14:47:35 -08:00
|
|
|
ev.ev.key.keysym.sym = (SDL_Keycode)(int)msg->sdlkey;
|
2010-06-01 17:36:21 -07:00
|
|
|
in_dispatch_event(&ev);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MESSAGEHANDLER(GuiCharEvent)
|
|
|
|
|
{
|
|
|
|
|
// wxWidgets has special Char events but SDL doesn't, so convert it to
|
|
|
|
|
// a keydown+keyup sequence. (We do the conversion here instead of on
|
|
|
|
|
// the wx side to avoid nondeterministic behaviour caused by async messaging.)
|
|
|
|
|
|
|
|
|
|
SDL_Event_ ev = { { 0 } };
|
|
|
|
|
ev.ev.type = SDL_KEYDOWN;
|
2012-02-06 14:47:35 -08:00
|
|
|
ev.ev.key.keysym.sym = (SDL_Keycode)(int)msg->sdlkey;
|
2010-06-01 17:36:21 -07:00
|
|
|
in_dispatch_event(&ev);
|
|
|
|
|
|
|
|
|
|
ev.ev.type = SDL_KEYUP;
|
|
|
|
|
in_dispatch_event(&ev);
|
|
|
|
|
}
|
2007-01-16 19:25:20 -08:00
|
|
|
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|