summaryrefslogtreecommitdiff
path: root/src/PowderToy.cpp
blob: 7ede4d5c1c4c403597c0d1e97a0937f348923505 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122

#include <time.h>
#include <SDL/SDL.h>
#include <iostream>
#include <sstream>
#include <string>
#include "Config.h"
#include "Simulation.h"
#include "Renderer.h"
#include "Graphics.h"
#include "Air.h"

#include "interface/Engine.h"
#include "interface/Button.h"
#include "interface/Sandbox.h"
#include "interface/Panel.h"
#include "interface/ControlFactory.h"
#include "interface/Point.h"
#include "interface/Label.h"
#include "GameSession.h"

using namespace std;

SDL_Surface * SDLOpen()
{
#if defined(WIN32) && defined(WINCONSOLE)
	FILE * console = fopen("CON", "w" );
#endif
	if (SDL_Init(SDL_INIT_VIDEO)<0)
	{
		fprintf(stderr, "Initializing SDL: %s\n", SDL_GetError());
		return 0;
	}
#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)
	{
		freopen("CON", "w", stdout);
		freopen("con", "w", stderr);
		fclose(console);
	}
#endif
	atexit(SDL_Quit);
	return SDL_SetVideoMode(XRES + BARSIZE, YRES + MENUSIZE, 32, SDL_SWSURFACE);
}

int SDLPoll(SDL_Event * event)
{
	event->type = 0;
	while (SDL_PollEvent(event))
	{
		switch (event->type)
		{
			case SDL_QUIT:
				return 1;
		}
	}
	return 0;
}

int main(int argc, char * argv[])
{
	int elapsedTime = 0, currentTime = 0, lastTime = 0, currentFrame = 0;
	float fps, fpsLimit, delta;

	//Renderer * ren;
	//Simulation * sim = new Simulation();
	//ren = new Renderer(g, sim);

	GameSession * gameSession = new GameSession();
	ui::Engine * engine = &ui::Engine::Ref();//new ui::Engine();
	ui::State * engineState = new ui::State();
	ui::Sandbox * sandbox = new ui::Sandbox();
	ui::Button * button = new ui::Button(ui::Point(100, 100), ui::Point(100, 100), std::string("poP"));
	ui::Label * fpsLabel = new ui::Label(ui::Point(2, 2), ui::Point(200, 14), std::string("FPS: 0"));
	engine->Begin(XRES, YRES, SDLOpen());
	engine->SetState(engineState);
	engineState->AddComponent(fpsLabel);
	engineState->AddComponent(sandbox);
	engineState->AddComponent(button);
	//window->Add(ControlFactory::MainMenu(gameSession, 0, 0, 200, 200));

	SDL_Event event;
	while(!SDLPoll(&event))
	{
		//mouseButton = SDL_GetMouseState(&mouseX, &mouseY);
		switch(event.type)
		{
		case SDL_KEYDOWN:
			break;
		case SDL_KEYUP:
			break;
		case SDL_MOUSEMOTION:
			engine->onMouseMove(event.motion.x, event.motion.y);
			break;
		case SDL_MOUSEBUTTONDOWN:
			engine->onMouseClick(event.motion.x, event.motion.y, event.button.button);
			break;
		case SDL_MOUSEBUTTONUP:
			engine->onMouseUnclick(event.motion.x, event.motion.y, event.button.button);
			break;
		}
		fpsLabel->LabelText = "";
		stringstream fpsText;
		fpsText << "FPS: " << fps;
		fpsLabel->LabelText = fpsText.str();

		engine->Tick(delta);
		engine->Draw();

		currentFrame++;
		currentTime = SDL_GetTicks();
		elapsedTime = currentTime - lastTime;
		if(elapsedTime>=1000)
		{
			fps = (((float)currentFrame)/((float)elapsedTime))*1000.0f;
			currentFrame = 0;
			lastTime = currentTime;
			delta = 60.0f/fps;
		}
	}
}