2012-05-30 07:17:40 -05:00
|
|
|
#ifdef USE_SDL
|
2012-01-08 11:39:03 -06:00
|
|
|
|
|
|
|
#include <time.h>
|
2012-05-13 11:43:41 -05:00
|
|
|
#include "SDL.h"
|
2012-01-29 11:54:53 -06:00
|
|
|
#ifdef WIN32
|
2012-05-13 11:43:41 -05:00
|
|
|
#include "SDL_syswm.h"
|
2012-01-29 11:54:53 -06:00
|
|
|
#endif
|
2012-01-14 12:51:24 -06:00
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
2012-01-08 11:39:03 -06:00
|
|
|
#include "Config.h"
|
|
|
|
#include "Graphics.h"
|
2012-01-29 11:54:53 -06:00
|
|
|
#if defined(LIN32) || defined(LIN64)
|
|
|
|
#include "icon.h"
|
|
|
|
#endif
|
2012-01-08 11:39:03 -06:00
|
|
|
|
2012-01-14 12:51:24 -06:00
|
|
|
#include "interface/Engine.h"
|
2012-01-08 11:39:03 -06:00
|
|
|
#include "interface/Button.h"
|
2012-01-10 17:18:37 -06:00
|
|
|
#include "interface/Panel.h"
|
|
|
|
#include "interface/ControlFactory.h"
|
2012-01-14 12:51:24 -06:00
|
|
|
#include "interface/Point.h"
|
|
|
|
#include "interface/Label.h"
|
2012-01-08 11:39:03 -06:00
|
|
|
|
2012-01-17 14:46:06 -06:00
|
|
|
#include "game/GameController.h"
|
|
|
|
#include "game/GameView.h"
|
|
|
|
|
2012-01-29 08:44:36 -06:00
|
|
|
#include "dialogues/ErrorMessage.h"
|
|
|
|
|
2012-01-19 07:44:59 -06:00
|
|
|
#include "client/HTTP.h"
|
|
|
|
|
2012-01-14 12:51:24 -06:00
|
|
|
using namespace std;
|
|
|
|
|
2012-01-29 11:54:53 -06:00
|
|
|
#ifdef WIN32
|
|
|
|
extern "C" IMAGE_DOS_HEADER __ImageBase;
|
|
|
|
#endif
|
|
|
|
|
2012-05-30 06:32:58 -05:00
|
|
|
SDL_Surface * sdl_scrn;
|
|
|
|
|
|
|
|
#ifdef OGLR
|
|
|
|
void blit()
|
|
|
|
{
|
|
|
|
SDL_GL_SwapBuffers();
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
void blit(pixel * vid)
|
|
|
|
{
|
|
|
|
if(sdl_scrn)
|
|
|
|
{
|
|
|
|
pixel * dst;
|
|
|
|
pixel * src = vid;
|
|
|
|
int j, x = 0, y = 0, w = XRES+BARSIZE, h = YRES+MENUSIZE, pitch = XRES+BARSIZE;
|
|
|
|
if (SDL_MUSTLOCK(sdl_scrn))
|
|
|
|
if (SDL_LockSurface(sdl_scrn)<0)
|
|
|
|
return;
|
|
|
|
dst=(pixel *)sdl_scrn->pixels+y*sdl_scrn->pitch/PIXELSIZE+x;
|
|
|
|
for (j=0; j<h; j++)
|
|
|
|
{
|
|
|
|
memcpy(dst, src, w*PIXELSIZE);
|
|
|
|
dst+=sdl_scrn->pitch/PIXELSIZE;
|
|
|
|
src+=pitch;
|
|
|
|
}
|
|
|
|
if (SDL_MUSTLOCK(sdl_scrn))
|
|
|
|
SDL_UnlockSurface(sdl_scrn);
|
|
|
|
SDL_UpdateRect(sdl_scrn,0,0,0,0);
|
|
|
|
}
|
|
|
|
}
|
2012-05-30 07:17:40 -05:00
|
|
|
#endif
|
2012-05-30 06:32:58 -05:00
|
|
|
|
2012-01-08 11:39:03 -06:00
|
|
|
SDL_Surface * SDLOpen()
|
|
|
|
{
|
2012-04-22 11:13:43 -05:00
|
|
|
SDL_Surface * surface;
|
2012-01-14 12:51:24 -06:00
|
|
|
#if defined(WIN32) && defined(WINCONSOLE)
|
|
|
|
FILE * console = fopen("CON", "w" );
|
|
|
|
#endif
|
2012-01-08 11:39:03 -06:00
|
|
|
if (SDL_Init(SDL_INIT_VIDEO)<0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Initializing SDL: %s\n", SDL_GetError());
|
|
|
|
return 0;
|
|
|
|
}
|
2012-01-24 17:33:32 -06:00
|
|
|
SDL_EnableUNICODE(1);
|
2012-01-14 12:51:24 -06:00
|
|
|
#if defined(WIN32) && defined(WINCONSOLE)
|
|
|
|
//On Windows, SDL redirects stdout to stdout.txt, which can be annoying when debugging, here we redirect back to the console
|
|
|
|
if (console)
|
|
|
|
{
|
2012-01-29 11:54:53 -06:00
|
|
|
|
2012-01-14 12:51:24 -06:00
|
|
|
freopen("CON", "w", stdout);
|
2012-02-05 10:37:36 -06:00
|
|
|
freopen("CON", "w", stderr);
|
|
|
|
//fclose(console);
|
2012-01-14 12:51:24 -06:00
|
|
|
}
|
|
|
|
#endif
|
2012-01-29 11:54:53 -06:00
|
|
|
#ifdef WIN32
|
|
|
|
SDL_SysWMinfo SysInfo;
|
|
|
|
SDL_VERSION(&SysInfo.version);
|
|
|
|
if(SDL_GetWMInfo(&SysInfo) <= 0) {
|
|
|
|
printf("%s : %d\n", SDL_GetError(), SysInfo.window);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
HWND WindowHandle = SysInfo.window;
|
|
|
|
HICON hIconSmall = (HICON)LoadImage(reinterpret_cast<HMODULE>(&__ImageBase), MAKEINTRESOURCE(101), IMAGE_ICON, 16, 16, LR_SHARED);
|
|
|
|
HICON hIconBig = (HICON)LoadImage(reinterpret_cast<HMODULE>(&__ImageBase), MAKEINTRESOURCE(101), IMAGE_ICON, 32, 32, LR_SHARED);
|
|
|
|
SendMessage(WindowHandle, WM_SETICON, ICON_SMALL, (LPARAM)hIconSmall);
|
|
|
|
SendMessage(WindowHandle, WM_SETICON, ICON_BIG, (LPARAM)hIconBig);
|
|
|
|
#elif defined(LIN32) || defined(LIN32)
|
|
|
|
SDL_Surface *icon = SDL_CreateRGBSurfaceFrom(app_icon, 16, 16, 32, 64, 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000);
|
|
|
|
SDL_WM_SetIcon(icon, NULL);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
SDL_WM_SetCaption("The Powder Toy", "Powder Toy");
|
2012-03-03 11:58:33 -06:00
|
|
|
//SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
|
2012-01-08 11:39:03 -06:00
|
|
|
atexit(SDL_Quit);
|
2012-04-20 13:21:10 -05:00
|
|
|
#ifndef OGLR
|
2012-04-22 11:13:43 -05:00
|
|
|
surface = SDL_SetVideoMode(XRES + BARSIZE, YRES + MENUSIZE, 32, SDL_SWSURFACE);
|
2012-04-20 13:21:10 -05:00
|
|
|
#else
|
2012-04-22 11:13:43 -05:00
|
|
|
surface = SDL_SetVideoMode(XRES + BARSIZE, YRES + MENUSIZE, 32, SDL_OPENGL);
|
2012-04-20 13:21:10 -05:00
|
|
|
#endif
|
2012-04-22 11:13:43 -05:00
|
|
|
|
2012-06-20 13:23:22 -05:00
|
|
|
#if defined(OGLR)
|
2012-04-22 11:13:43 -05:00
|
|
|
int status = glewInit();
|
|
|
|
if(status != GLEW_OK)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Initializing Glew: %d\n", status);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return surface;
|
2012-01-08 11:39:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char * argv[])
|
|
|
|
{
|
2012-01-14 12:51:24 -06:00
|
|
|
int elapsedTime = 0, currentTime = 0, lastTime = 0, currentFrame = 0;
|
2012-06-20 07:40:18 -05:00
|
|
|
unsigned int lastTick = 0;
|
2012-01-24 14:19:19 -06:00
|
|
|
float fps = 0, delta = 1.0f;
|
2012-01-08 11:39:03 -06:00
|
|
|
|
2012-05-30 06:32:58 -05:00
|
|
|
sdl_scrn = SDLOpen();
|
|
|
|
#ifdef OGLR
|
|
|
|
SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1);
|
|
|
|
#endif
|
|
|
|
|
2012-01-17 14:46:06 -06:00
|
|
|
ui::Engine::Ref().g = new Graphics();
|
2012-05-30 06:32:58 -05:00
|
|
|
//ui::Engine::Ref().g->AttachSDLSurface(SDLOpen());
|
2012-01-15 13:35:40 -06:00
|
|
|
|
2012-01-20 16:07:49 -06:00
|
|
|
ui::Engine * engine = &ui::Engine::Ref();
|
2012-01-21 17:29:40 -06:00
|
|
|
engine->Begin(XRES+BARSIZE, YRES+MENUSIZE);
|
2012-01-17 14:46:06 -06:00
|
|
|
|
|
|
|
GameController * gameController = new GameController();
|
|
|
|
engine->ShowWindow(gameController->GetView());
|
2012-01-08 11:39:03 -06:00
|
|
|
|
2012-01-29 11:12:35 -06:00
|
|
|
//new ErrorMessage("Error", "This is a test error message");
|
2012-01-29 08:44:36 -06:00
|
|
|
|
2012-01-08 11:39:03 -06:00
|
|
|
SDL_Event event;
|
2012-01-15 13:35:40 -06:00
|
|
|
while(engine->Running())
|
2012-01-08 11:39:03 -06:00
|
|
|
{
|
2012-01-15 13:35:40 -06:00
|
|
|
event.type = 0;
|
|
|
|
while (SDL_PollEvent(&event))
|
2012-01-08 11:39:03 -06:00
|
|
|
{
|
2012-01-15 13:35:40 -06:00
|
|
|
switch (event.type)
|
|
|
|
{
|
|
|
|
case SDL_QUIT:
|
|
|
|
engine->Exit();
|
|
|
|
break;
|
|
|
|
case SDL_KEYDOWN:
|
2012-01-29 18:40:28 -06:00
|
|
|
engine->onKeyPress(event.key.keysym.sym, event.key.keysym.unicode, event.key.keysym.mod&KEY_MOD_SHIFT, event.key.keysym.mod&KEY_MOD_CONTROL, event.key.keysym.mod&KEY_MOD_ALT);
|
2012-01-15 13:35:40 -06:00
|
|
|
break;
|
|
|
|
case SDL_KEYUP:
|
2012-01-29 18:40:28 -06:00
|
|
|
engine->onKeyRelease(event.key.keysym.sym, event.key.keysym.unicode, event.key.keysym.mod&KEY_MOD_SHIFT, event.key.keysym.mod&KEY_MOD_CONTROL, event.key.keysym.mod&KEY_MOD_ALT);
|
2012-01-15 13:35:40 -06:00
|
|
|
break;
|
|
|
|
case SDL_MOUSEMOTION:
|
|
|
|
engine->onMouseMove(event.motion.x, event.motion.y);
|
|
|
|
break;
|
|
|
|
case SDL_MOUSEBUTTONDOWN:
|
2012-01-21 12:51:28 -06:00
|
|
|
if(event.button.button == SDL_BUTTON_WHEELUP)
|
|
|
|
{
|
|
|
|
engine->onMouseWheel(event.motion.x, event.motion.y, 1);
|
|
|
|
}
|
|
|
|
else if (event.button.button == SDL_BUTTON_WHEELDOWN)
|
|
|
|
{
|
|
|
|
engine->onMouseWheel(event.motion.x, event.motion.y, -1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
engine->onMouseClick(event.motion.x, event.motion.y, event.button.button);
|
|
|
|
}
|
2012-01-15 13:35:40 -06:00
|
|
|
break;
|
|
|
|
case SDL_MOUSEBUTTONUP:
|
2012-01-21 12:51:28 -06:00
|
|
|
if(event.button.button != SDL_BUTTON_WHEELUP && event.button.button != SDL_BUTTON_WHEELDOWN)
|
|
|
|
engine->onMouseUnclick(event.motion.x, event.motion.y, event.button.button);
|
2012-01-15 13:35:40 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
event.type = 0; //Clear last event
|
2012-01-08 11:39:03 -06:00
|
|
|
}
|
|
|
|
|
2012-01-31 12:49:14 -06:00
|
|
|
engine->Tick();
|
2012-01-14 12:51:24 -06:00
|
|
|
engine->Draw();
|
2012-05-30 06:32:58 -05:00
|
|
|
|
2012-06-20 07:40:18 -05:00
|
|
|
if(SDL_GetTicks()-lastTick>1000)
|
|
|
|
{
|
|
|
|
//Run client tick every second
|
|
|
|
lastTick = SDL_GetTicks();
|
|
|
|
Client::Ref().Tick();
|
|
|
|
}
|
|
|
|
|
2012-05-30 06:32:58 -05:00
|
|
|
#ifdef OGLR
|
|
|
|
blit();
|
|
|
|
#else
|
|
|
|
blit(engine->g->vid);
|
|
|
|
#endif
|
2012-01-08 11:39:03 -06:00
|
|
|
|
2012-01-14 12:51:24 -06:00
|
|
|
currentFrame++;
|
|
|
|
currentTime = SDL_GetTicks();
|
|
|
|
elapsedTime = currentTime - lastTime;
|
2012-01-24 14:19:19 -06:00
|
|
|
if(ui::Engine::Ref().FpsLimit > 2.0f && (currentFrame>2 || elapsedTime > 1000*2/ui::Engine::Ref().FpsLimit) && elapsedTime && currentFrame*1000/elapsedTime > ui::Engine::Ref().FpsLimit)
|
2012-01-15 13:35:40 -06:00
|
|
|
{
|
|
|
|
while (currentFrame*1000/elapsedTime > ui::Engine::Ref().FpsLimit)
|
|
|
|
{
|
|
|
|
SDL_Delay(1);
|
|
|
|
currentTime = SDL_GetTicks();
|
|
|
|
elapsedTime = currentTime-lastTime;
|
|
|
|
}
|
|
|
|
}
|
2012-01-14 12:51:24 -06:00
|
|
|
if(elapsedTime>=1000)
|
|
|
|
{
|
|
|
|
fps = (((float)currentFrame)/((float)elapsedTime))*1000.0f;
|
|
|
|
currentFrame = 0;
|
|
|
|
lastTime = currentTime;
|
2012-01-24 17:33:32 -06:00
|
|
|
if(ui::Engine::Ref().FpsLimit > 2.0f)
|
|
|
|
{
|
|
|
|
delta = ui::Engine::Ref().FpsLimit/fps;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
delta = 1.0f;
|
|
|
|
}
|
2012-01-14 12:51:24 -06:00
|
|
|
}
|
2012-01-31 12:49:14 -06:00
|
|
|
engine->SetFps(fps);
|
2012-01-08 11:39:03 -06:00
|
|
|
}
|
2012-01-21 07:19:10 -06:00
|
|
|
ui::Engine::Ref().CloseWindow();
|
|
|
|
delete gameController;
|
|
|
|
delete ui::Engine::Ref().g;
|
2012-05-30 07:17:40 -05:00
|
|
|
return 0;
|
2012-01-08 11:39:03 -06:00
|
|
|
}
|
2012-05-30 07:17:40 -05:00
|
|
|
|
|
|
|
#endif
|