forked from Landama01/MiniJoc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
239 lines (206 loc) · 6.12 KB
/
Game.cpp
File metadata and controls
239 lines (206 loc) · 6.12 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include "Game.h"
#include <math.h>
Game::Game() {}
Game::~Game(){}
bool Game::Init()
{
//Initialize SDL with all subsystems
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
SDL_Log("Unable to initialize SDL: %s", SDL_GetError());
return false;
}
//Create our window: title, x, y, w, h, flags
Window = SDL_CreateWindow("Spaceship: arrow keys + space", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN);
if (Window == NULL)
{
SDL_Log("Unable to create window: %s", SDL_GetError());
return false;
}
//Create a 2D rendering context for a window: window, device index, flags
Renderer = SDL_CreateRenderer(Window, -1, 0);
if (Renderer == NULL)
{
SDL_Log("Unable to create rendering context: %s", SDL_GetError());
return false;
}
//Initialize keys array
for (int i = 0; i < MAX_KEYS; ++i)
keys[i] = KEY_IDLE;
//Load images
if (!LoadImages())
return false;
//Init Image
//Init variables
Player.Init(20, WINDOW_HEIGHT >> 1, 104, 82, 5);
idx_shot = 0;
int w;
SDL_QueryTexture(img_background, NULL, NULL, &w, NULL);
Scene.Init(0, 0, 1024, WINDOW_HEIGHT, 4);
return true;
}
bool Game::LoadImages()
{
if (IMG_Init(IMG_INIT_PNG) != IMG_INIT_PNG)
{
SDL_Log("IMG_Init, failed to init required png support: %s\n", IMG_GetError());
return false;
}
//escenaris
img_background = SDL_CreateTextureFromSurface(Renderer, IMG_Load("escenari_1.png"));
if (img_background == NULL) {
SDL_Log("CreateTextureFromSurface failed: %s\n", SDL_GetError());
return false;
}
//luffys
img_player = SDL_CreateTextureFromSurface(Renderer, IMG_Load("luffynormal.png"));
if (img_player == NULL) {
SDL_Log("CreateTextureFromSurface failed: %s\n", SDL_GetError());
return false;
}
img_luffy2 = SDL_CreateTextureFromSurface(Renderer, IMG_Load("luffy2normal.png"));
if (img_luffy2 == NULL) {
SDL_Log("CreateTextureFromSurface failed: %s\n", SDL_GetError());
return false;
}
img_luffy3 = SDL_CreateTextureFromSurface(Renderer, IMG_Load("luffy3normal.png"));
if (img_luffy3 == NULL) {
SDL_Log("CreateTextureFromSurface failed: %s\n", SDL_GetError());
return false;
}
img_luffy4 = SDL_CreateTextureFromSurface(Renderer, IMG_Load("luffy4normal.png"));
if (img_luffy4 == NULL) {
SDL_Log("CreateTextureFromSurface failed: %s\n", SDL_GetError());
return false;
}
//punys
img_shot = SDL_CreateTextureFromSurface(Renderer, IMG_Load("puny1.png"));
if (img_shot == NULL) {
SDL_Log("CreateTextureFromSurface failed: %s\n", SDL_GetError());
return false;
}
img_puny2 = SDL_CreateTextureFromSurface(Renderer, IMG_Load("atac2.png"));
if (img_puny2 == NULL) {
SDL_Log("CreateTextureFromSurface failed: %s\n", SDL_GetError());
return false;
}
img_puny3 = SDL_CreateTextureFromSurface(Renderer, IMG_Load("puny3.png"));
if (img_puny3 == NULL) {
SDL_Log("CreateTextureFromSurface failed: %s\n", SDL_GetError());
return false;
}
img_puny4 = SDL_CreateTextureFromSurface(Renderer, IMG_Load("puny3.png"));
if (img_puny4 == NULL) {
SDL_Log("CreateTextureFromSurface failed: %s\n", SDL_GetError());
return false;
}
return true;
}
void Game::Release()
{
SDL_DestroyTexture(img_background);
SDL_DestroyTexture(img_player);
SDL_DestroyTexture(img_luffy2);
SDL_DestroyTexture(img_luffy3);
SDL_DestroyTexture(img_luffy4);
SDL_DestroyTexture(img_shot);
SDL_DestroyTexture(img_puny2);
SDL_DestroyTexture(img_puny3);
SDL_DestroyTexture(img_puny4);
IMG_Quit();
//Clean up all SDL initialized subsystems
SDL_Quit();
}
bool Game::Input()
{
SDL_Event event;
if (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT) return false;
}
SDL_PumpEvents();
const Uint8* keyboard = SDL_GetKeyboardState(NULL);
for (int i = 0; i < MAX_KEYS; ++i)
{
if (keyboard[i])
keys[i] = (keys[i] == KEY_IDLE) ? KEY_DOWN : KEY_REPEAT;
else
keys[i] = (keys[i] == KEY_REPEAT || keys[i] == KEY_DOWN) ? KEY_UP : KEY_IDLE;
}
return true;
}
bool Game::Update()
{
//Read Input
if (!Input()) return true;
//Process Input
int fx = 0, fy = 0;
if (keys[SDL_SCANCODE_ESCAPE] == KEY_DOWN) return true;
if (keys[SDL_SCANCODE_UP] == KEY_REPEAT) fy = -1;
if (keys[SDL_SCANCODE_DOWN] == KEY_REPEAT) fy = 1;
if (keys[SDL_SCANCODE_LEFT] == KEY_REPEAT) fx = -1;
if (keys[SDL_SCANCODE_RIGHT] == KEY_REPEAT) fx = 1;
if (keys[SDL_SCANCODE_SPACE] == KEY_DOWN)
{
int x, y, w, h;
Player.GetRect(&x, &y, &w, &h);
Shots1[idx_shot].Init(x + w - 75, y + (h >> 1) - 38, 56, 20, 10);
Shots2[idx_shot].Init(x + w - 75, y + (h >> 1) + 17, 56, 20, 10);
idx_shot++;
idx_shot %= MAX_SHOTS;
}
//Logic
//Scene.Move(-1, 0);
//if (Scene.GetX() <= -Scene.GetWidth()) Scene.SetX(0);
//Player update
Player.Move(fx, fy);
//Shots update
for (int i = 0; i < MAX_SHOTS; ++i)
{
if (Shots1[i].IsAlive())
{
Shots1[i].Move(1, 0);
if (Shots1[i].GetX() > WINDOW_WIDTH) Shots1[i].ShutDown();
}
if (Shots2[i].IsAlive())
{
Shots2[i].Move(1, 0);
if (Shots2[i].GetX() > WINDOW_WIDTH) Shots2[i].ShutDown();
}
}
return false;
}
void Game::Draw()
{
SDL_Rect rc;
//Set the color used for drawing operations
SDL_SetRenderDrawColor(Renderer, 0, 0, 0, 255);
//Clear rendering target
SDL_RenderClear(Renderer);
//Draw scene
Scene.GetRect(&rc.x, &rc.y, &rc.w, &rc.h);
SDL_RenderCopy(Renderer, img_background, NULL, &rc);
rc.x += rc.w;
SDL_RenderCopy(Renderer, img_background, NULL, &rc);
//Draw player
Player.GetRect(&rc.x, &rc.y, &rc.w, &rc.h);
SDL_RenderCopy(Renderer, img_player, NULL, &rc);
//SDL_RenderFillRect(Renderer, &rc);
//Draw shots
SDL_SetRenderDrawColor(Renderer, 192, 0, 0, 255);
for (int i = 0; i < MAX_SHOTS; ++i)
{
if (Shots1[i].IsAlive())
{
Shots1[i].GetRect(&rc.x, &rc.y, &rc.w, &rc.h);
SDL_RenderCopy(Renderer, img_shot, NULL, &rc);
}
if (Shots2[i].IsAlive())
{
Shots2[i].GetRect(&rc.x, &rc.y, &rc.w, &rc.h);
SDL_RenderCopy(Renderer, img_shot, NULL, &rc);
}
}
//Update screen
SDL_RenderPresent(Renderer);
SDL_Delay(10); // 1000/10 = 100 fps max
}