Progress:

1. Switched back to floating point coordinates
2. Implemented eight way movement
3. Added window icon
4. Bunch of fixes
This commit is contained in:
MGislv 2024-05-12 18:46:34 +02:00
parent a8fabba611
commit 1ea1be96d1
11 changed files with 116 additions and 110 deletions

View File

@ -1,3 +0,0 @@
/*#include "asset_manager.h"
int **/

View File

@ -14,33 +14,33 @@
typedef struct window {
SDL_Window *window;
SDL_Renderer *renderer;
int width;
int height;
int width;
int height;
} Gm_Window;
typedef struct colors {
SDL_Color white;
SDL_Color black;
SDL_Color white;
SDL_Color black;
} Gm_Colors;
typedef struct resources {
TTF_Font *font;
Gm_Colors colors;
TTF_Font *font;
Gm_Colors colors;
} Gm_resources;
typedef struct player {
double x;
double y;
int life;
int alive;
float x;
float y;
int life;
int alive;
} Player;
typedef struct enemy {
double x;
double y;
double dest_x;
double dest_y;
int alive;
float x;
float y;
int dest_x;
int dest_y;
int alive;
} Enemy;
// Func prototypes

View File

@ -19,12 +19,14 @@ int init(Gm_Window *window, SDL_DisplayMode *ds_mode)
SDL_GetCurrentDisplayMode(0, ds_mode);
// TODO: Check if screen is large enough
window->window = SDL_CreateWindow("SDL2 Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, window->width, window->height, 0);
window->window = SDL_CreateWindow("SDL2 Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, window->width, window->height, 0);
if (window->window == NULL) {
SDL_Log("SDL Error (window creation): %s\n", SDL_GetError());
return -1;
}
SDL_SetWindowIcon(window->window, IMG_Load("icon.png"));
window->renderer = SDL_CreateRenderer(window->window, -1, 0);
if (window->renderer == NULL) {
SDL_Log("SDL Error (render init): %s\n", SDL_GetError());

View File

View File

@ -2,16 +2,7 @@
#include "menu.h"
#include "render.h"
#include "audio.h"
/*Uint64 time_left(Uint64 ticks)
{
Uint64 now = SDL_GetTicks64();
if (ticks <= now) {
return 0;
} else {
return ticks - now;
}
}*/
#include "movement.h"
int main(int argc, char *argv[])
{
@ -20,6 +11,7 @@ int main(int argc, char *argv[])
Gm_Window window;
SDL_DisplayMode mode;
Gm_resources resources;
const Uint8 *keys = SDL_GetKeyboardState(NULL);
if (init(&window, &mode) != 0) {
SDL_Log("Can't init SDL");
@ -27,7 +19,7 @@ int main(int argc, char *argv[])
}
// Common entities
Player player = { 0.0, 0.0, 100, 1 };
Player player = { 0, 0, 100, 1 };
SDL_Rect player_rect;
Enemy enemyarray[random() % 10 + 1];
@ -39,8 +31,11 @@ int main(int argc, char *argv[])
#ifdef DEBUG
SDL_Log("Loaded texture size: %d x %d", player_rect.w, player_rect.h);
#endif
player_rect.x = player.x = (double)((window.width / 2) - (player_rect.w / 2));
player_rect.y = player.y = (double)((window.height / 2) - (player_rect.h / 2));
player.x = window.width / 2.0f - player_rect.w / 2.0f;
player.y = window.height / 2.0f - player_rect.h / 2.0f;
player_rect.x = (int)player.x;
player_rect.y = (int)player.y;
enemy_rect.w /= 8;
enemy_rect.h /= 8;
@ -53,8 +48,8 @@ int main(int argc, char *argv[])
// Randomize enemy positions
for (int i = 0; i < (int)(sizeof(enemyarray) / sizeof(Enemy)); i++) {
//enemyarray[i].x = (int)(random() % window.width);
enemyarray[i].x = -40.0;
enemyarray[i].y = (double)(random() % window.height);
enemyarray[i].x = -60.0f;
enemyarray[i].y = (float)(random() % window.height);
}
resources.font = TTF_OpenFont("Roboto-Medium.ttf", 18);
@ -74,20 +69,26 @@ int main(int argc, char *argv[])
running = main_menu(&window, &resources, &event);
// AAAAAAAAA
playSound("floppa.wav", SDL_MIX_MAXVOLUME);
if (running) {
renderText(&window, &resources, "PRESS ANY KEY TO START", window.width / 2, window.height / 2, 1, resources.colors.white, resources.colors.black);
} else {
renderText(&window, &resources, "BYE", window.width / 2, window.height / 2, 1, resources.colors.white, resources.colors.black);
}
SDL_RenderPresent(window.renderer);
do {
SDL_PollEvent(&event);
} while (event.type != SDL_KEYDOWN);
// AAAAAAAAA
playSound("floppa.wav", SDL_MIX_MAXVOLUME * 0.5);
renderText(&window, &resources, "PRESS ANY KEY TO START", window.width / 2,
window.height / 2, 1, resources.colors.white, resources.colors.black);
SDL_RenderPresent(window.renderer);
SDL_SetRenderDrawColor(window.renderer, (Uint8)(random() % 255), (Uint8)(random() % 255), (Uint8)(random() % 255), 255);
// Wait for input
do {
SDL_PollEvent(&event);
} while (event.type != SDL_KEYDOWN);
} else {
goto quit;
}
// Create background with random color
SDL_SetRenderDrawColor(window.renderer,
(Uint8)(random() % 255),
(Uint8)(random() % 255),
(Uint8)(random() % 255), 255);
SDL_RenderFillRect(window.renderer, NULL);
int fps_cap = 0;
@ -104,44 +105,27 @@ int main(int argc, char *argv[])
SDL_PollEvent(&event);
switch (event.type) {
case SDL_QUIT:
quit:
SDL_RenderClear(window.renderer);
SDL_SetRenderDrawColor(window.renderer, 0, 0, 0, 255);
SDL_RenderFillRect(window.renderer, NULL);
renderText(&window, &resources, "BYE", window.width / 2, window.height / 2, 1, resources.colors.white, resources.colors.black);
renderText(&window, &resources, "BYE", window.width / 2, window.height / 2,
1, resources.colors.white, resources.colors.black);
SDL_RenderPresent(window.renderer);
SDL_Delay(1000);
SDL_Log("Program quit after %i ticks", event.quit.timestamp);
running = 0;
quit(&window);
break;
case SDL_KEYUP:
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
case SDLK_DOWN:
player.y += (double)(now_ticks - old_ticks) / 1000.0 + 10.0;
break;
case SDLK_UP:
player.y -= (double)(now_ticks - old_ticks) / 1000.0 + 10.0;
break;
case SDLK_LEFT:
player.x -= (double)(now_ticks - old_ticks) / 1000.0 + 10.0;
break;
case SDLK_RIGHT:
player.x += (double)(now_ticks - old_ticks) / 1000.0 + 10.0;
break;
case SDLK_F1:
if (fps_cap) {
fps_cap = 0;
} else {
fps_cap = 1;
}
break;
default:
break;
}
handle_keys(keys, &event, &player, now_ticks, old_ticks);
// Ever chaning background
//SDL_SetRenderDrawColor(window.renderer, (Uint8)(random() % 255), (Uint8)(random() % 255), (Uint8)(random() % 255), 255);
//SDL_SetRenderDrawColor(window.renderer,
// (Uint8)(random() % 255),
// (Uint8)(random() % 255),
// (Uint8)(random() % 255), 255);
//SDL_RenderFillRect(window.renderer, NULL);
break;
@ -149,27 +133,30 @@ int main(int argc, char *argv[])
break;
}
player_rect.x = roundf(player.x);
player_rect.y = roundf(player.y);
player_rect.x = (int)roundf(player.x);
player_rect.y = (int)roundf(player.y);
#ifdef DEBUG
SDL_Log("Player position: %f x %f\nInteger position: %d x %d", player.x, player.y, player_rect.x, player_rect.y);
#endif
SDL_RenderCopy(window.renderer, player_texture, NULL, &player_rect);
for (int i = 0; i < (int)(sizeof(enemyarray) / sizeof(Enemy)); i++) {
enemyarray[i].x += (double)(now_ticks - old_ticks) / 1000.0 + 0.01;
enemyarray[i].y += (double)(now_ticks - old_ticks) / 1000.0 + 0.01;
enemy_rect.x = roundf(enemyarray[i].x);
enemy_rect.y = roundf(enemyarray[i].y);
enemyarray[i].x += (float)((Uint64)(now_ticks - old_ticks) / 1000.0f + 0.02f);
enemyarray[i].y += (float)((Uint64)(now_ticks - old_ticks) / 1000.0f + 0.02f);
enemy_rect.x = (int)roundf(enemyarray[i].x);
enemy_rect.y = (int)roundf(enemyarray[i].y);
SDL_RenderCopy(window.renderer, player_texture, NULL, &enemy_rect);
}
if (fps_cap) {
end = SDL_GetPerformanceCounter();
double elapsed = (double)(end - start) / (double)SDL_GetPerformanceFrequency();
float elapsed = (float)((Uint64)(end - start) / (float)SDL_GetPerformanceFrequency());
#ifdef DEBUG
SDL_Log("Current FPS: %f", (double)(1.0 / elapsed));
SDL_Log("Current FPS: %f", 1 / elapsed);
#endif
char fps_str[10];
snprintf(fps_str, 10, "%d", (int)(1.0 / elapsed));
snprintf(fps_str, 10, "%d", (int)(1 / elapsed));
renderText(&window, &resources, fps_str, 10, 10, 0, resources.colors.white, resources.colors.black);
}
@ -177,9 +164,6 @@ int main(int argc, char *argv[])
SDL_RenderPresent(window.renderer);
old_ticks = now_ticks;
//SDL_Delay(time_left(ticks));
//ticks += 30;
}
quit(&window);

View File

@ -14,10 +14,6 @@ int main_menu(Gm_Window *window, Gm_resources *resources, SDL_Event *event)
switch (event->type) {
case SDL_QUIT:
quit:
SDL_Log("Program quit after %i ticks", event->quit.timestamp);
SDL_SetRenderDrawColor(window->renderer, 0, 0, 0, 255);
SDL_RenderFillRect(window->renderer, NULL);
SDL_RenderPresent(window->renderer);
return 0;
case SDL_KEYDOWN:
switch (event->key.keysym.sym) {
@ -57,21 +53,23 @@ quit:
default:
break;
}
break;
default:
break;
}
// Draw
renderImage(window, "title.png", 3, window->width / 2, window->height / 4, 1);
for (int i = 0; i < (int)(sizeof(selection) / sizeof(int)); i++) {
if (selection[i]) {
renderText(window, resources, options[i], window->width / 2,
(window->height / 2) + i * 40, 1,
window->height / 2 + i * 40, 1,
resources->colors.black, resources->colors.white);
} else {
renderText(window, resources, options[i], window->width / 2,
(window->height / 2) + i * 40, 1,
window->height / 2 + i * 40, 1,
resources->colors.white, resources->colors.black);
}
}

View File

@ -1,6 +0,0 @@
/*#include "common.h"
void find_nextXY()
{
}*/

22
src/movement.c Normal file
View File

@ -0,0 +1,22 @@
#include "movement.h"
#define SPEED 15
void handle_keys(const Uint8 *keys, SDL_Event *event, Player *player, Uint64 now_ticks, Uint64 old_ticks)
{
if (keys[SDL_SCANCODE_DOWN]) {
player->y += (float)((Uint64)(now_ticks - old_ticks) / 1000.0f + SPEED);
}
if (keys[SDL_SCANCODE_UP]) {
player->y -= (float)((Uint64)(now_ticks - old_ticks) / 1000.0f + SPEED);
}
if (keys[SDL_SCANCODE_LEFT]) {
player->x -= (float)((Uint64)(now_ticks - old_ticks) / 1000.0f + SPEED);
}
if (keys[SDL_SCANCODE_RIGHT]) {
player->x += (float)((Uint64)(now_ticks - old_ticks) / 1000.0f + SPEED);
}
}

9
src/movement.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef _MOVEMENT_H
#define _MOVEMENT_H
#include "common.h"
// Func prototypes
void handle_keys(const Uint8 *keys, SDL_Event *event, Player *player, Uint64 now_ticks, Uint64 old_ticks);
#endif

View File

@ -1,6 +1,6 @@
#include "render.h"
void renderText(Gm_Window *window, Gm_resources *resources, const char *str, int x, int y, int centered, SDL_Color fg_color, SDL_Color bg_color)
void renderText(Gm_Window *window, Gm_resources *resources, const char *str, float x, float y, int centered, SDL_Color fg_color, SDL_Color bg_color)
{
SDL_Surface *str_surface = TTF_RenderUTF8_LCD(resources->font, str, fg_color, bg_color);
SDL_Texture *str_texture = SDL_CreateTextureFromSurface(window->renderer, str_surface);
@ -13,11 +13,11 @@ void renderText(Gm_Window *window, Gm_resources *resources, const char *str, int
// Draw from center or top left
if (centered) {
str_rect.x = x - str_rect.w / 2;
str_rect.y = y - str_rect.h / 2;
str_rect.x = (int)roundf(x - str_rect.w / 2.0f);
str_rect.y = (int)roundf(y - str_rect.h / 2.0f);
} else {
str_rect.x = x;
str_rect.y = y;
str_rect.x = (int)roundf(x);
str_rect.y = (int)roundf(y);
}
//SDL_FillRect(str_surface, &str, SDL_MapRGBA(fps_surf->format, 255, 255, 255, 255));
@ -28,7 +28,7 @@ void renderText(Gm_Window *window, Gm_resources *resources, const char *str, int
SDL_FreeSurface(str_surface);
}
void renderImage(Gm_Window *window, char *path, float factor, int x, int y, int centered)
void renderImage(Gm_Window *window, char *path, float factor, float x, float y, int centered)
{
SDL_Texture *texture = IMG_LoadTexture(window->renderer, path);
SDL_SetTextureScaleMode(texture, SDL_ScaleModeNearest);
@ -38,16 +38,16 @@ void renderImage(Gm_Window *window, char *path, float factor, int x, int y, int
SDL_Log("Loaded texture size: %d x %d", texture_rect.w, texture_rect.h);
#endif
texture_rect.w = (int)((float)texture_rect.w * factor);
texture_rect.h = (int)((float)texture_rect.h * factor);
texture_rect.w = (int)(texture_rect.w * factor);
texture_rect.h = (int)(texture_rect.h * factor);
// Draw from center or top left
if (centered) {
texture_rect.x = x - texture_rect.w / 2;
texture_rect.y = y - texture_rect.h / 2;
texture_rect.x = (int)roundf(x - texture_rect.w / 2.0f);
texture_rect.y = (int)roundf(y - texture_rect.h / 2.0f);
} else {
texture_rect.x = x;
texture_rect.y = y;
texture_rect.x = (int)roundf(x);
texture_rect.y = (int)roundf(y);
}
SDL_RenderCopy(window->renderer, texture, NULL, &texture_rect);

View File

@ -4,7 +4,7 @@
#include "common.h"
// Func prototypes
void renderText(Gm_Window *window, Gm_resources *resources, const char *str, int x, int y, int centered, SDL_Color fg_color, SDL_Color bg_color);
void renderImage(Gm_Window *window, char *path, float factor, int x, int y, int centered);
void renderText(Gm_Window *window, Gm_resources *resources, const char *str, float x, float y, int centered, SDL_Color fg_color, SDL_Color bg_color);
void renderImage(Gm_Window *window, char *path, float factor, float x, float y, int centered);
#endif