This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void render() { | |
// Clear screen | |
SDL_RenderClear(gRenderer); | |
// Render zombies | |
for (int i = 0; i < ZOMBIE_COUNT; i++) { | |
if (zombies[i].alive) { | |
SDL_Rect srcZombie = { .x = (zombies[i].frameX / zombieAnimSpeed) * zombieWidth, .y = zombies[i].frameY * zombieHeight, .w = zombieWidth, .h = zombieHeight }; | |
SDL_Rect dstZombie = { .x = (int)zombies[i].x, .y = (int)zombies[i].y, .w = zombieWidth, .h = zombieHeight }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const int ZOMBIE_COUNT = 20; | |
const int SPAWN_FREQ = 3; | |
unsigned int lastSpawnTime = 0, currentTime; | |
int zombieAnimSpeed = 8; | |
int zombieSpeed = 3; | |
SDL_Texture* zombieTexture; | |
struct Zombie { | |
float x, y; | |
int w = 64; // Width and height have been moved here for a cleaner code | |
int h = 64; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Check collision between two boxes (http://lazyfoo.net/tutorials/SDL/27_collision_detection/index.php) | |
bool collision(float xA, float xB, float yA, float yB, int wA, int wB, int hA, int hB) { | |
if (yA + hA <= yB) { | |
return false; | |
} | |
if (yA >= yB + hB) { | |
return false; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// If survivor is alive.. | |
if (survivor.state != "state_dead") { | |
... | |
// If survivor is stabbing.. | |
if (survivor.state == "state_stab") { | |
// If we're at the third animation frame, and attack has not been delivered yet.. | |
if (survivor.frameX / survivor.animSpeed == 2 && !survivor.stab) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For each alive zombie.. | |
for (int i = 0; i < ZOMBIE_COUNT; i++) { | |
if (zombies[i].alive) { | |
// Out of screen -> dead | |
if (zombies[i].y > SCREEN_HEIGHT) { | |
zombies[i].alive = false; | |
} | |
// Apply gravity |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For each alive bullet.. | |
for (int i = 0; i < BULLET_COUNT; i++) { | |
if (bullets[i].alive) { | |
bullets[i].x += bulletSpeed * bullets[i].dir; | |
// Out of screen -> dead | |
if (bullets[i].x + bullets[i].w < 0 || bullets[i].x > SCREEN_WIDTH) { | |
bullets[i].alive = false; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Font | |
TTF_Font* gFont = NULL; | |
// Score | |
int score = 0; | |
SDL_Texture* scoreTexture; | |
int scoreWidth, scoreHeight; | |
// Highscore | |
Sint32 highScore; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void loadMedia() { | |
// Init font | |
gFont = TTF_OpenFont("assets/3Dventure.ttf", 28); | |
if (gFont == NULL) { | |
printf("Failed to load font! SDL_ttf Error: %s\n", TTF_GetError()); | |
} | |
// Load high score from file (http://lazyfoo.net/tutorials/SDL/33_file_reading_and_writing/index.php) | |
SDL_RWops* file = SDL_RWFromFile("score.bin", "r+b"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Restart game | |
void restart() { | |
survivor.x = 200; | |
survivor.y = 100; | |
survivor.vX = 0; | |
survivor.vY = 0; | |
survivor.scaleX = 1; | |
survivor.frameX = 0; | |
survivor.frameY = 0; | |
survivor.alive = true; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void update() { | |
// If survivor is dead.. | |
if (survivor.state == "state_dead") { | |
// Check if "R" key is pressed and restart | |
const Uint8* keys = SDL_GetKeyboardState(NULL); | |
if (keys[SDL_SCANCODE_R]) { | |
restart(); | |
} |