-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
172 lines (127 loc) · 3.53 KB
/
main.cpp
File metadata and controls
172 lines (127 loc) · 3.53 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <string>
#include <assert.h>
#include "Timer.h"
#include <vector>
#include <string>
#include <sstream>
#include "GameModelController.h"
#include "Loader.h"
#include "IFetcher.h"
#include "Fetcher.h"
#define WIDTH 1305
#define HEIGHT 734
//Screen attributes
const int SCREEN_WIDTH = 1305;
const int SCREEN_HEIGHT = 734;
const int SCREEN_BPP = 32;
const int FRAME_DIMENSION = 120;
#define VIDEOWIDTH 1305
#define VIDEOHEIGHT 734
//The frames per second
const int FRAMES_PER_SECOND = 30;
int counter = 0;
//The surfaces
SDL_Surface *background = NULL;
SDL_Surface *message = NULL;
SDL_Surface *screen = NULL;
static GameModelController* controller = NULL;
static SDL_mutex *mutex = NULL;
static bool quit = false;
static SDL_Thread *threadChargementImage = NULL;
static bool imageChargee = false;
SDL_Event event;
int SDLCALL fonctionChargementImage(void *data)
{
while(1)
{
if(imageChargee == false)
{
// LOCK
if ( SDL_mutexP(mutex) < 0 ) {
fprintf(stderr, "Couldn't lock mutex: %s", SDL_GetError());
exit(1);
}
controller->chargeSurface();
imageChargee = true;
// UNLOCK
if ( SDL_mutexV(mutex) < 0 ) {
fprintf(stderr, "Couldn't unlock mutex: %s", SDL_GetError());
exit(1);
}
}
SDL_Delay(10);
}
}
int main( int argc, char* args[] )
{
// Direction, position, grid_dimension,
GameModel* gameModel = new GameModel(IDLE, 1, 625,
"images/water",
"images/missile"
);
Fetcher* fetcher = new Fetcher(30, 30,
"nothing");
Loader* loader = new Loader(fetcher, 7, 30);
loader->initialize(1);
/*
* Initialise libSDL
*/
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTTHREAD) == -1)
{
printf("cannot initialize SDL\n");
return EXIT_FAILURE;
}
SDL_Surface *screen, *empty, *frame_surface, *layer;
empty = SDL_CreateRGBSurface(SDL_SWSURFACE, VIDEOWIDTH, VIDEOHEIGHT,
32, 0, 0, 0, 0);
mutex = SDL_CreateMutex();
//int options = SDL_ANYFORMAT | SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_FULLSCREEN;
int options = SDL_ANYFORMAT | SDL_HWSURFACE | SDL_DOUBLEBUF;
screen = SDL_SetVideoMode(WIDTH, HEIGHT, 0, options);
if(!screen)
{
printf("cannot set video mode\n");
return EXIT_FAILURE;
}
controller = new GameModelController((IGameModel*)gameModel, (ILoader*)loader, screen, empty);
// Création du thread pour charger les images
if ( (threadChargementImage = SDL_CreateThread(fonctionChargementImage, NULL)) == NULL ) {
printf("Impossible de créer le thread --> Chargement image\n");
return EXIT_FAILURE;
}
//Quit flag
quit = false;
//The frame rate regulator
Timer fps;
while( quit == false ) {
//Start the frame timer
fps.start();
quit = controller->handleKeyEvent();
if(quit == true) {
printf("FIN ...\n");
}
if(imageChargee == true && quit == false) {
controller->applySurfaces();
controller->flipSurfaces();
controller->nextPosition();
controller->freeSurfaces();
if ( SDL_mutexP(mutex) < 0 ) {
fprintf(stderr, "Couldn't lock mutex: %s", SDL_GetError());
exit(1);
}
imageChargee = false;
if ( SDL_mutexV(mutex) < 0 ) {
fprintf(stderr, "Couldn't lock mutex: %s", SDL_GetError());
exit(1);
}
if( fps.get_ticks() < (1000 / FRAMES_PER_SECOND) )
{
//Sleep the remaining frame time
SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
}
}
}
return 0;
}