forked from mr-andreas/neuralnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gfx.cpp
142 lines (113 loc) · 3.38 KB
/
gfx.cpp
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "gfx.h"
#include <SDL/SDL_rotozoom.h>
#include <SDL/SDL_timer.h>
#include <math.h>
const int FRAMES_PER_SECOND = 60;
const int FRAMERATE_IN_MS = 1000/FRAMES_PER_SECOND;
const int REPOPULATE_ON_FRAME = 60*FRAMES_PER_SECOND;
const char* WINDOW_TITLE = "SDL Start";
void plotSweeper(sdlgamestate_t *g, const Sweeper &sweeper) {
// Part of the bitmap that we want to draw
SDL_Rect source;
source.x = 0;
source.y = 0;
source.w = 32;
source.h = 32;
// Part of the screen we want to draw the sprite to
SDL_Rect destination;
destination.x = sweeper.posx-11;
destination.y = sweeper.posy-13;
destination.w = 32;
destination.h = 32;
SDL_Surface *rotated = rotozoomSurface(g->bitmaps.sweeper, sweeper.rotation*180.0/M_PI-90.0, 0.5, 1);
SDL_BlitSurface(rotated, &source, g->screen, &destination);
SDL_FreeSurface(rotated);
}
void plotSweepers(sdlgamestate_t *g) {
const std::vector<Sweeper> &s(g->gamestate->sweepers);
for(std::vector<Sweeper>::const_iterator i = s.begin(); i != s.end(); i++) {
plotSweeper(g, *i);
}
}
void plotMines(const sdlgamestate_t *g) {
const std::vector<Mine> &s(g->gamestate->mines);
for(std::vector<Mine>::const_iterator i = s.begin(); i != s.end(); i++) {
// Part of the bitmap that we want to draw
SDL_Rect source;
source.x = 0;
source.y = 0;
source.w = 5;
source.h = 5;
// Part of the screen we want to draw the sprite to
SDL_Rect destination;
destination.x = i->posx-2;
destination.y = i->posy-2;
destination.w = 5;
destination.h = 5;
SDL_BlitSurface(g->bitmaps.mine, &source, g->screen, &destination);
}
}
void init(sdlgamestate_t *g) {
SDL_Init( SDL_INIT_VIDEO );
g->screen = SDL_SetVideoMode(
g->gamestate->boardWidth, g->gamestate->boardHeight, 0, SDL_HWSURFACE | SDL_DOUBLEBUF
);
SDL_WM_SetCaption( WINDOW_TITLE, 0 );
g->bitmaps.sweeper = IMG_Load("/home/ante/dev/neuralnet/res/sweeper.png");
if(!g->bitmaps.sweeper) {
printf("No bitmap :(\n");
exit(1);
}
g->bitmaps.mine = IMG_Load("/home/ante/dev/neuralnet/res/mine.png");
}
int sdlMainLoop(sdlgamestate_t *g) {
init(g);
SDL_Event event;
bool gameRunning = true;
int plotPause = 0;
unsigned int startTime, endTime;
unsigned int frameCounter = 0;
int delay;
while (gameRunning) {
startTime = SDL_GetTicks();
if (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
gameRunning = false;
}
if(event.type == SDL_KEYUP) {
if(event.key.keysym.sym == 'q') {
gameRunning = false;
}
if(plotPause) {
plotPause = 0;
} else {
if(event.key.keysym.sym == 'n') {
plotPause = 1;
} else {
plotPause = 2;
}
}
}
}
SDL_FillRect(g->screen, NULL, SDL_MapRGB(g->screen->format, 255, 255, 255));
if(frameCounter++ % REPOPULATE_ON_FRAME == 0) {
printf("Transplanting brains\n");
brainTransplant(g->gamestate);
if(plotPause == 1)
plotPause = 0;
}
doTurn(g->gamestate);
if(!plotPause) {
plotMines(g);
plotSweepers(g);
SDL_Flip(g->screen);
endTime = SDL_GetTicks();
delay = FRAMERATE_IN_MS - (endTime - startTime);
if(delay > 0)
SDL_Delay(delay);
}
}
SDL_FreeSurface(g->bitmaps.sweeper);
SDL_Quit();
return 0;
}