Skip to content

Commit d88ae73

Browse files
author
David Ghiassi
committed
Added 720p mode for resolutions between 720p and 1080p
1 parent d0c46dc commit d88ae73

File tree

22 files changed

+269
-112
lines changed

22 files changed

+269
-112
lines changed

src/background.c

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ void init_background(struct window *window)
1010
window->stars->next = NULL;
1111

1212
// Create some initial points
13-
for (int c = 0; c < 8 * window->h; c++)
13+
for (int c = 0; c < 4 * DEFAULT_H; c++)
1414
{
1515
struct point *new = xmalloc(sizeof(struct point), window->window);
1616

17-
new->x = rand() % window->w;
18-
new->y = rand() % window->h;
17+
new->x = rand() % DEFAULT_W;
18+
new->y = rand() % DEFAULT_H;
1919
new->z = (rand() % 5) + 2;
2020
new->opacity = rand() % 256;
2121

@@ -29,8 +29,8 @@ void init_background(struct window *window)
2929

3030
void move_background(struct window *window, unsigned long framecount)
3131
{
32-
struct point *last = window->stars;
3332
struct point *p = window->stars->next;
33+
struct point *last = window->stars;
3434

3535
while (p)
3636
{
@@ -53,12 +53,12 @@ void move_background(struct window *window, unsigned long framecount)
5353
}
5454

5555
// Create some points
56-
for (int c = 0; c < rand() % 6; c++)
56+
for (int c = 0; c < rand() % 3; c++)
5757
{
5858
struct point *new = xmalloc(sizeof(struct point), window->window);
5959

60-
new->x = window->w - 1;
61-
new->y = rand() % window->h;
60+
new->x = DEFAULT_W - 1;
61+
new->y = rand() % DEFAULT_H;
6262
new->z = (rand() % 5) + 2;
6363
new->opacity = rand() % 256;
6464

@@ -77,8 +77,14 @@ void render_background(struct window *window)
7777

7878
while (p)
7979
{
80-
SDL_SetRenderDrawColor(window->renderer, p->opacity, p->opacity, p->opacity, p->opacity);
81-
SDL_RenderDrawPoint(window->renderer, p->x, p->y);
80+
SDL_SetRenderDrawColor(window->renderer, p->opacity,
81+
p->opacity, p->opacity, p->opacity);
82+
83+
SDL_Rect pos = { .x = p->x, .y = p->y, .w = 3, .h = 3 };
84+
85+
resize_pos_for_resolution(window, &pos);
86+
87+
SDL_RenderDrawPoint(window->renderer, pos.x, pos.y);
8288

8389
p = p->next;
8490
}

src/boss.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
void set_boss_attributes(struct list *new, SDL_Rect *pos, struct window *window)
1010
{
11-
init_position(window->w, pos->y, window, window->img->boss->texture, &new->pos_dst);
11+
init_position(window->w, pos->y, window->img->boss->texture, &new->pos_dst);
1212

1313
new->speed.x = window->paths->data[window->paths->index].line.enemy_path.speed_x;
1414
new->speed.y = window->paths->data[window->paths->index].line.enemy_path.speed_x;
@@ -33,7 +33,10 @@ void create_boss(struct window *window)
3333
int h = 0;
3434
SDL_QueryTexture(window->img->boss->texture, NULL, NULL, NULL, &h);
3535

36-
SDL_Rect pos = { .x = 0, .y = window->paths->data[window->paths->index].line.enemy_path.pos_y - h / 2, .w = 0, .h = 0 };
36+
SDL_Rect pos = { .x = 0,
37+
.y = window->paths->data[window->paths->index].line.enemy_path.pos_y - h / 2,
38+
.w = 0,
39+
.h = 0 };
3740

3841
list_push_front(&pos, window, BOSS_LIST, NULL, NULL, 0, 0);
3942

@@ -50,14 +53,14 @@ void move_boss(struct window *window, SDL_Rect *ship_pos)
5053
while (temp)
5154
{
5255
// move boss
53-
if (temp->pos_dst.x + temp->pos_dst.w + 60 > window->w)
56+
if (temp->pos_dst.x + temp->pos_dst.w + 60 > DEFAULT_W)
5457
temp->pos_dst.x -= temp->speed.x;
5558
else
5659
{
5760
temp->pos_dst.y += temp->speed.y;
5861

5962
// If vertical out of bounds, change vertical speed
60-
if (temp->pos_dst.y < 60 || temp->pos_dst.y + temp->pos_dst.h > window->h - 60)
63+
if (temp->pos_dst.y < 60 || temp->pos_dst.y + temp->pos_dst.h > DEFAULT_H - 60)
6164
temp->speed.y = -temp->speed.y;
6265
}
6366

@@ -77,6 +80,14 @@ void render_boss(struct window *window)
7780

7881
while (temp)
7982
{
83+
SDL_Rect pos = { .x = temp->pos_dst.x,
84+
.y = temp->pos_dst.y,
85+
.w = temp->pos_dst.w,
86+
.h = temp->pos_dst.h
87+
};
88+
89+
resize_pos_for_resolution(window, &pos);
90+
8091
SDL_RenderCopy(window->renderer, window->img->boss->texture, NULL, &temp->pos_dst);
8192

8293
// Go to next boss

src/credits.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ static int render_screen(struct window *window, char screen[][CREDITS_COLS],
5555
.y = UP_PADDING + line * CREDITS_CHAR_H,
5656
.w = CREDITS_CHAR_W,
5757
.h = CREDITS_CHAR_H };
58+
59+
resize_pos_for_resolution(window, &pos);
60+
5861
SDL_RenderFillRect(window->renderer, &pos);
5962
}
6063

src/enemy.c

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include <stdlib.h>
99
#include <math.h>
1010
#include <SDL2/SDL.h>
11-
#include <SDL2/SDL2_gfxPrimitives.h>
1211

1312

1413
static int is_rotating(char enemy_type)
@@ -50,7 +49,7 @@ void set_enemy_attributes(struct list *new, SDL_Rect *pos,
5049

5150
new->rotating = is_rotating(enemy_type);
5251
new->curr_texture = 0;
53-
init_position(window->w, pos->y, window,
52+
init_position(DEFAULT_W, pos->y,
5453
new->rotating ? new->texture.textures[0]->texture : new->texture.texture->texture,
5554
&new->pos_dst);
5655
}
@@ -87,7 +86,12 @@ void create_enemies(struct window *window)
8786
break;
8887
}
8988

90-
SDL_Rect pos = { .x = 0, .y = window->paths->data[window->paths->index].line.enemy_path.pos_y - h / 2, .w = 0, .h = 0 };
89+
SDL_Rect pos = { .x = 0,
90+
.y = window->paths->data[window->paths->index].line.enemy_path.pos_y
91+
- h / 2,
92+
.w = 0,
93+
.h = 0
94+
};
9195

9296
char type = window->paths->data[window->paths->index].line.enemy_path.enemy_type;
9397

@@ -182,14 +186,26 @@ void render_enemies(struct window *window)
182186
.h = h
183187
};
184188

189+
resize_pos_for_resolution(window, &pos);
190+
185191
SDL_RenderCopy(window->renderer,
186192
temp->texture.textures[temp->curr_texture]->texture,
187193
NULL, &pos);
188194
}
189195
else
196+
{
197+
SDL_Rect pos = { .x = temp->pos_dst.x,
198+
.y = temp->pos_dst.y,
199+
.w = temp->pos_dst.w,
200+
.h = temp->pos_dst.h
201+
};
202+
203+
resize_pos_for_resolution(window, &pos);
204+
190205
SDL_RenderCopy(window->renderer,
191206
temp->texture.texture->texture,
192-
NULL, &temp->pos_dst);
207+
NULL, &pos);
208+
}
193209

194210
// Go to next enemy
195211
temp = temp->next;
@@ -202,19 +218,29 @@ void render_enemies(struct window *window)
202218

203219
void render_enemy_health(struct window *window, struct list *enemy)
204220
{
205-
boxRGBA(window->renderer,
206-
enemy->pos_dst.x + enemy->pos_dst.w / 2 - 50,
207-
enemy->pos_dst.y - 25,
208-
enemy->pos_dst.x + enemy->pos_dst.w / 2 - 50 + (100 * enemy->health) / enemy->max_health,
209-
enemy->pos_dst.y - 20,
210-
0, 255, 0, 192);
211-
212-
boxRGBA(window->renderer,
213-
enemy->pos_dst.x + enemy->pos_dst.w / 2 - 50 + (100 * enemy->health) / enemy->max_health,
214-
enemy->pos_dst.y - 25,
215-
enemy->pos_dst.x + enemy->pos_dst.w / 2 + 50,
216-
enemy->pos_dst.y - 20,
217-
255, 0, 0, 192);
221+
SDL_Rect pos = { .x = enemy->pos_dst.x + enemy->pos_dst.w / 2 - 50,
222+
.y = enemy->pos_dst.y - 25,
223+
.w = (100 * enemy->health) / enemy->max_health,
224+
.h = 5
225+
};
226+
227+
int old_w = pos.w;
228+
229+
resize_pos_for_resolution(window, &pos);
230+
231+
SDL_SetRenderDrawColor(window->renderer, 0, 255, 0, 192); // green
232+
SDL_RenderFillRect(window->renderer, &pos);
233+
234+
235+
pos.x = enemy->pos_dst.x + enemy->pos_dst.w / 2 - 50 + (100 * enemy->health) / enemy->max_health;
236+
pos.y = enemy->pos_dst.y - 25; // Mandatory because of the resize_pos_for_resolution
237+
pos.w = 100 - old_w;
238+
pos.h = 5; // Mandatory because of the resize_pos_for_resolution
239+
240+
resize_pos_for_resolution(window, &pos);
241+
242+
SDL_SetRenderDrawColor(window->renderer, 255, 0, 0, 192); // red
243+
SDL_RenderFillRect(window->renderer, &pos);
218244
}
219245

220246

@@ -269,7 +295,7 @@ void move_enemy_shots(struct window *window)
269295
// Prevent out of bounds by deleting the shot if not on screen
270296
if (temp->pos_dst.x + temp->pos_dst.w <= 0
271297
|| temp->pos_dst.y + temp->pos_dst.h <= 0
272-
|| temp->pos_dst.y >= window->h)
298+
|| temp->pos_dst.y >= DEFAULT_H)
273299
{
274300
struct list *to_delete = temp;
275301
prev->next = temp->next;
@@ -293,8 +319,16 @@ void render_enemy_shots(struct window *window)
293319

294320
while (temp)
295321
{
322+
SDL_Rect pos = { .x = temp->pos_dst.x,
323+
.y = temp->pos_dst.y,
324+
.w = temp->pos_dst.w,
325+
.h = temp->pos_dst.h
326+
};
327+
328+
resize_pos_for_resolution(window, &pos);
329+
296330
// Display shot
297-
SDL_RenderCopy(window->renderer, window->img->enemy_shot->texture, NULL, &temp->pos_dst);
331+
SDL_RenderCopy(window->renderer, window->img->enemy_shot->texture, NULL, &pos);
298332

299333
// Go to next shot
300334
temp = temp->next;

src/explosion.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "init.h"
22
#include "explosion.h"
3+
#include "utils.h"
34
#include <SDL2/SDL.h>
45

56

@@ -66,8 +67,16 @@ void render_explosions(struct window *window)
6667

6768
while (temp)
6869
{
70+
SDL_Rect pos = { .x = temp->pos_dst.x,
71+
.y = temp->pos_dst.y,
72+
.w = temp->pos_dst.w,
73+
.h = temp->pos_dst.h
74+
};
75+
76+
resize_pos_for_resolution(window, &pos);
77+
6978
// Display shot
70-
SDL_RenderCopy(window->renderer, window->img->explosion, &temp->pos_src, &temp->pos_dst);
79+
SDL_RenderCopy(window->renderer, window->img->explosion, &temp->pos_src, &pos);
7180

7281
// Go to next shot
7382
temp = temp->next;

src/game.c

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ static void handle_arrow_event(struct window *window, SDL_Rect *pos)
5858
// Prevent out of bounds
5959
if (pos->y < 0)
6060
pos->y = 0;
61-
else if (pos->y > window->h - pos->h)
62-
pos->y = window->h - pos->h;
61+
else if (pos->y > DEFAULT_H - pos->h)
62+
pos->y = DEFAULT_H - pos->h;
6363

6464
if (pos->x < 0)
6565
pos->x = 0;
66-
else if (pos->x > window->w - pos->w)
67-
pos->x = window->w - pos->w;
66+
else if (pos->x > DEFAULT_W - pos->w)
67+
pos->x = DEFAULT_W - pos->w;
6868
}
6969

7070

@@ -136,6 +136,8 @@ void render_trail(struct window *window, SDL_Rect *pos, int is_enemy)
136136
else
137137
pos_dst_trail.x -= 5;
138138

139+
resize_pos_for_resolution(window, &pos_dst_trail);
140+
139141
SDL_RenderCopyEx(window->renderer, window->img->trail, NULL, &pos_dst_trail,
140142
0, NULL, flip);
141143
}
@@ -158,7 +160,7 @@ static int respawn(struct window *window, SDL_Rect *pos)
158160
window->health = window->max_health;
159161

160162
window->respawn_frame = 0;
161-
init_position(120, POS_CENTERED, window, window->img->ship->texture, pos);
163+
init_position(120, POS_CENTERED, window->img->ship->texture, pos);
162164

163165
window->lives--;
164166
}
@@ -226,6 +228,20 @@ void reset_game_attributes(struct window *window, int difficulty, int all_reset)
226228
}
227229

228230

231+
static void render_ship(struct window *window, SDL_Rect *temp_pos)
232+
{
233+
SDL_Rect pos = { .x = temp_pos->x,
234+
.y = temp_pos->y,
235+
.w = temp_pos->w,
236+
.h = temp_pos->h
237+
};
238+
239+
resize_pos_for_resolution(window, &pos);
240+
241+
SDL_RenderCopy(window->renderer, window->img->ship->texture, NULL, &pos);
242+
}
243+
244+
229245
void play_game(struct window *window, int mission_num, int difficulty)
230246
{
231247
int is_arcade = 0;
@@ -255,7 +271,7 @@ void play_game(struct window *window, int mission_num, int difficulty)
255271
load_music(window, "data/madness.ogg", 1);
256272
init_background(window);
257273

258-
init_position(120, POS_CENTERED, window, window->img->ship->texture, &pos);
274+
init_position(120, POS_CENTERED, window->img->ship->texture, &pos);
259275

260276
retry = 0;
261277
}
@@ -306,7 +322,7 @@ void play_game(struct window *window, int mission_num, int difficulty)
306322
if (window->health > 0)
307323
{
308324
render_shield_aura(window, &pos);
309-
SDL_RenderCopy(window->renderer, window->img->ship->texture, NULL, &pos);
325+
render_ship(window, &pos);
310326
}
311327
render_explosions(window);
312328
render_hud_texts(window);

0 commit comments

Comments
 (0)