Skip to content

Commit

Permalink
Merge pull request #10 from ryanpcmcquen/refactor
Browse files Browse the repository at this point in the history
Refactor movement, collision detection, and memory allocation.
  • Loading branch information
ryanpcmcquen authored Jul 13, 2020
2 parents 1ed56aa + 20f7834 commit 58bd56d
Show file tree
Hide file tree
Showing 14 changed files with 417 additions and 346 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ Basque is a cross-platform top-down 2d game engine.

---
Running on Windows:

![Basque Windows demo](demos/basque_windows_demo.gif 'Basque Windows demo')

Running on Mac:

![Basque Mac demo](demos/basque_mac_demo.gif 'Basque Mac demo')

Running on Linux:

![Basque Linux demo](demos/basque_linux_demo.gif 'Basque Linux demo')

---
Expand Down Expand Up @@ -69,6 +72,7 @@ Why not just use Godot/Unity/et cetera? Basque has a very different priority lis
- Resolution detection (estimates window size to 80% of the screen or uses the full resolution for fullscreen mode, this can be switched off).
- High DPI awareness. By default the game will not run in high DPI mode, but it will be aware of high DPI contexts. This prevents things from looking fuzzy with weird scaling settings.
- Automated GitHub and GitLab packaging pipelines.
- Scancode based movement (rather than event loop key states). This keeps the controls from being susceptible to operating system key repeat rates and delays.

Everything that is here should not be considered a final or optimal solution, but rather, a simple approach that can be morphed into a better solution. At this point the engine will become more specific to the game I am building, which is why I see this as the best time to open source it. Hopefully, it can be useful to others, either as a starting point, or as a reference of some things you can accomplish with SDL2.

Expand Down
6 changes: 3 additions & 3 deletions assets/data/map_layout.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
_, 27, 16, 16, 16, 16, 28, 16, 16, 17
_, 27, 16, 16, 16, 16, 28, 16, 16, 17
_, 48, 15, 28, 28, 28, 28, 16, 16, 17
_, _, 27, 16, 16, 28, 16, 16, 16, 37, 1, 5
_, _, 48, 49, 50, 15, 16, 16, 16, 16, 16, 17
_, _, 27, 16, 16, 28, 16, 28, 16, 37, 1, 5
_, _, 48, 49, 50, 15, 16, 16, 28, 16, 16, 17
_, _, _, _, _, 27, 16, 16, 16, 16, 16, 37, 5
_, _, _, _, _, 48, 49, 15, 28, 16, 16, 16, 17
_, _, _, _, _, _, _, 24, 16, 16, 28, 16, 37, 5
_, _, _, _, _, _, _, 24, 16, 16, 16, 28, 16, 37, 5
_, _, _, _, _, _, _, 24, 16, 16, 16, 16, 16, 37, 5
_, _, _, _, _, _, _, 48, 50, 50, 15, 16, 16, 16, 37, 1, 5
_, _, _, _, _, _, _, _, _, _, 48, 15, 16, 16, 16, 16, 17
_, _, _, _, _, _, _, _, _, _, _, 48, 15, 16, 16, 16, 37, 1, 1, 1, 1, 1, 1, 1, 1, 5
Expand Down
10 changes: 5 additions & 5 deletions assets/data/map_layout_0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
27, 28, 16, 13, 14, 15, 17, _
27, 28, 16, 25, 26, 27, 17, _
27, 28, 28, 37, 38, 39, 17, _
27, 28, 28, 28, 28, 17, 17, _
48, 15, 28, 28, 28, 17, 37, 1, 2, 5, _
27, 28, 28, 28, 28, 28, 17, _
48, 15, 28, 28, 28, 28, 37, 1, 2, 5, _
_, 27, 16, 16, 16, 28, 16, 16, 16, 17
_, 27, 16, 16, 16, 28, 16, 16, 16, 17
_, 27, 16, 16, 16, 28, 16, 28, 16, 17
Expand All @@ -14,12 +14,12 @@
_, 27, 16, 16, 16, 16, 28, 16, 16, 17
_, 27, 16, 16, 16, 16, 28, 16, 16, 17
_, 48, 15, 28, 28, 28, 28, 16, 16, 17
_, _, 27, 16, 16, 28, 16, 16, 16, 37, 1, 5
_, _, 48, 49, 50, 15, 16, 16, 16, 16, 16, 17
_, _, 27, 16, 16, 28, 16, 28, 16, 37, 1, 5
_, _, 48, 49, 50, 15, 16, 16, 28, 16, 16, 17
_, _, _, _, _, 27, 16, 16, 16, 16, 16, 37, 5
_, _, _, _, _, 48, 49, 15, 28, 16, 16, 16, 17
_, _, _, _, _, _, _, 24, 16, 16, 28, 16, 37, 5
_, _, _, _, _, _, _, 24, 16, 16, 16, 28, 16, 37, 5
_, _, _, _, _, _, _, 24, 16, 16, 16, 16, 16, 37, 5
_, _, _, _, _, _, _, 48, 50, 50, 15, 16, 16, 16, 37, 1, 5
_, _, _, _, _, _, _, _, _, _, 48, 15, 16, 16, 16, 16, 17
_, _, _, _, _, _, _, _, _, _, _, 48, 15, 16, 16, 16, 37, 1, 1, 1, 1, 1, 1, 1, 1, 5
Expand Down
6 changes: 3 additions & 3 deletions assets/data/map_layout_1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
27, 28, 16, 13, 14, 15, 17, _
27, 28, 16, 25, 26, 27, 17, _
27, 28, 28, 37, 38, 39, 17, _
27, 28, 28, 28, 28, 17, 17, _
27, 28, 28, 28, 28, 28, 17, _
48, 15, 28, 28, 28, 28, 37, 1, 2, 5, _
_, 27, 16, 16, 16, 28, 16, 16, 16, 17
_, 27, 16, 16, 16, 28, 16, 16, 16, 17
Expand All @@ -15,11 +15,11 @@
_, 27, 16, 16, 16, 16, 28, 16, 16, 17
_, 48, 15, 28, 28, 28, 28, 16, 16, 17
_, _, 27, 16, 16, 28, 16, 16, 16, 37, 1, 5
_, _, 48, 49, 50, 15, 16, 16, 16, 16, 16, 17
_, _, 48, 49, 50, 15, 16, 16, 28, 16, 16, 17
_, _, _, _, _, 27, 16, 16, 16, 16, 16, 37, 5
_, _, _, _, _, 48, 49, 15, 28, 16, 16, 16, 17
_, _, _, _, _, _, _, 24, 16, 16, 28, 16, 37, 5
_, _, _, _, _, _, _, 24, 16, 16, 16, 28, 16, 37, 5
_, _, _, _, _, _, _, 24, 16, 16, 16, 16, 16, 37, 5
_, _, _, _, _, _, _, 48, 50, 50, 15, 16, 16, 16, 37, 1, 5
_, _, _, _, _, _, _, _, _, _, 48, 15, 16, 16, 16, 16, 17
_, _, _, _, _, _, _, _, _, _, _, 48, 15, 16, 16, 16, 37, 1, 1, 1, 1, 1, 1, 1, 1, 5
Expand Down
6 changes: 3 additions & 3 deletions assets/data/map_layout_2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
_, 27, 16, 16, 16, 16, 28, 16, 16, 17
_, 48, 15, 28, 28, 28, 28, 16, 16, 17
_, _, 27, 16, 16, 28, 16, 16, 16, 37, 1, 5
_, _, 48, 49, 50, 15, 16, 16, 16, 16, 16, 17
_, _, _, _, _, 27, 16, 16, 16, 16, 16, 37, 5
_, _, 48, 49, 50, 15, 16, 16, 28, 16, 16, 17
_, _, _, _, _, 27, 16, 16, 34, 16, 16, 37, 5
_, _, _, _, _, 48, 49, 15, 28, 16, 16, 16, 17
_, _, _, _, _, _, _, 24, 16, 16, 28, 16, 37, 5
_, _, _, _, _, _, _, 24, 16, 16, 16, 28, 16, 37, 5
_, _, _, _, _, _, _, 24, 16, 16, 16, 16, 16, 37, 5
_, _, _, _, _, _, _, 48, 50, 50, 15, 16, 16, 16, 37, 1, 5
_, _, _, _, _, _, _, _, _, _, 48, 15, 16, 16, 16, 16, 17
_, _, _, _, _, _, _, _, _, _, _, 48, 15, 16, 16, 16, 37, 1, 1, 1, 1, 1, 1, 1, 1, 5
Expand Down
4 changes: 2 additions & 2 deletions assets/data/map_layout_3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
_, 27, 16, 16, 16, 16, 28, 16, 16, 17
_, 48, 15, 28, 28, 28, 28, 16, 16, 17
_, _, 27, 16, 16, 28, 16, 16, 16, 37, 1, 5
_, _, 48, 49, 50, 15, 16, 16, 16, 16, 16, 17
_, _, 48, 49, 50, 15, 16, 16, 28, 16, 16, 17
_, _, _, _, _, 27, 16, 16, 16, 16, 16, 37, 5
_, _, _, _, _, 48, 49, 15, 28, 16, 16, 16, 17
_, _, _, _, _, _, _, 24, 16, 16, 28, 16, 37, 5
_, _, _, _, _, _, _, 24, 16, 16, 16, 28, 16, 37, 5
_, _, _, _, _, _, _, 24, 16, 16, 16, 16, 16, 37, 5
_, _, _, _, _, _, _, 48, 50, 50, 15, 16, 16, 16, 37, 1, 5
_, _, _, _, _, _, _, _, _, _, 48, 15, 16, 16, 16, 16, 17
_, _, _, _, _, _, _, _, _, _, _, 48, 15, 16, 16, 16, 37, 1, 1, 1, 1, 1, 1, 1, 1, 5
Expand Down
8 changes: 4 additions & 4 deletions line_count.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
cloc|github.com/AlDanial/cloc v 1.86 T=0.02 s (338.4 files/s, 64055.5 lines/s)
cloc|github.com/AlDanial/cloc v 1.82 T=0.01 s (604.4 files/s, 120195.0 lines/s)
--- | ---

Language|files|blank|comment|code
:-------|-------:|-------:|-------:|-------:
C/C++ Header|6|208|113|985
C|1|5|0|14
C/C++ Header|6|235|154|983
C|1|6|0|14
--------|--------|--------|--------|--------
SUM:|7|213|113|999
SUM:|7|241|154|997
1 change: 1 addition & 0 deletions source/basque.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

int main(int argc, char* argv[])
{

for (int argument_index = 0; argument_index < argc; argument_index++) {
if (strcmp(argv[argument_index], "-f") == 0 || strcmp(argv[argument_index], "--fullscreen") == 0) {
FULLSCREEN_MODE = 1;
Expand Down
23 changes: 16 additions & 7 deletions source/configuration.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
#define GAME_TITLE "Basque"
#define DEBUG_MODE 1

int SCREEN_HEIGHT = 600;
int SCREEN_WIDTH = 800;
// int SCREEN_HEIGHT = 630;
// int SCREEN_WIDTH = 1120;
// int SCREEN_HEIGHT = 600;
// int SCREEN_WIDTH = 800;

int SCREEN_HEIGHT = 864;
int SCREEN_WIDTH = 704;

int DETECT_RESOLUTION = 1;
int FULLSCREEN_MODE = 0;

Expand All @@ -18,7 +24,10 @@ int FULLSCREEN_MODE = 0;
#define TILE_SPRITE_WIDTH 16
#define TILE_CHAR_LIMIT 4

#define SCALING 3
// This is a float, but we enforce integers
// with SDL_RenderSetIntegerScale().
#define INITIAL_SCALING 2.0
float SCALING = INITIAL_SCALING;

#define TXT_EXTENSION ".txt"
#define ASSETS_DIR "assets/"
Expand All @@ -37,7 +46,7 @@ int FULLSCREEN_MODE = 0;
#define BACKGROUND_IMAGE IMAGE_DIR "tiles_packed.png"

#define MUSIC_INTRO_FILE AUDIO_DIR "Basque_sample_music.ogg"
#define MUSIC_VOLUME (MIX_MAX_VOLUME / 8)
#define MUSIC_VOLUME (MIX_MAX_VOLUME / 4)
// #define MUSIC_VOLUME 0

#define EDITOR_FONT FONT_DIR "BPtypewrite.ttf"
Expand All @@ -46,12 +55,12 @@ int FULLSCREEN_MODE = 0;
#define EDITOR_FONT_DEST_SIZE_H 9
#define EDITOR_FONT_DEST_SIZE_W 10

#define NUMBER_OF_TILES 500
#define TILE_ATTRIBUTES_LIMIT 500

#define EMPTY_COLUMN -1
#define END_OF_ROW -2

#define PLAYER_INCREMENT 1
#define GLOBAL_INCREMENT (PLAYER_INCREMENT * (SCALING + 1))
#define GLOBAL_INCREMENT (PLAYER_INCREMENT * ((int)INITIAL_SCALING + 1))

#define MIN_FRAMETIME_MSECS 30
#define MIN_FRAMETIME_MSECS 22
38 changes: 17 additions & 21 deletions source/initialization.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,12 @@ void cleanup(void)

#define create_outlined_font(game, map_tile_str, font_outline_surface, font_surface, font_outline_color, font_color, font_rect) \
{ \
\
font_outline_surface = TTF_RenderText_Blended(game.font_outline, map_tile_str, font_outline_color); \
font_surface = TTF_RenderText_Blended(game.font, map_tile_str, font_color); \
font_rect.x = EDITOR_FONT_OUTLINE; \
font_rect.y = EDITOR_FONT_OUTLINE; \
font_rect.w = font_surface->w; \
font_rect.h = font_surface->h; \
\
SDL_SetSurfaceBlendMode(font_surface, SDL_BLENDMODE_BLEND); \
SDL_BlitSurface(font_surface, NULL, font_outline_surface, &font_rect); \
}
Expand All @@ -62,17 +60,19 @@ int init()
SCREEN_WIDTH = display_mode.w;
SCREEN_HEIGHT = display_mode.h;
} else {
SCREEN_WIDTH = display_mode.w * 0.8;
SCREEN_HEIGHT = display_mode.h * 0.8;
SCREEN_WIDTH = (int)(display_mode.w * 0.8);
SCREEN_HEIGHT = (int)(display_mode.h * 0.8);
}
}
SDL_Log("Detected screen width: %i\nDetected screen height: %i\n", display_mode.w, display_mode.h);
SDL_Log("Detected resolution: %ix%i\n", display_mode.w, display_mode.h);
}

app.window = SDL_CreateWindow(GAME_TITLE, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, (int)SCREEN_WIDTH, (int)SCREEN_HEIGHT, SDL_WINDOW_RESIZABLE);
// SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_RESIZABLE);

if (app.window != NULL) {
app.renderer = SDL_CreateRenderer(app.window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
// app.renderer = SDL_CreateRenderer(app.window, -1, SDL_RENDERER_ACCELERATED);

if (app.renderer != NULL) {

Expand All @@ -81,8 +81,9 @@ int init()
SDL_SetWindowFullscreen(app.window, SDL_WINDOW_FULLSCREEN);
}

SDL_RenderSetScale(app.renderer, SCALING, SCALING);
SDL_Log("Basque started with %ix scaling.", SCALING);
SDL_RenderSetIntegerScale(app.renderer, true);
SDL_RenderSetScale(app.renderer, INITIAL_SCALING, INITIAL_SCALING);
SDL_Log("%s started with %0.1fx scaling.", GAME_TITLE, INITIAL_SCALING);
// Set initial draw color:
SDL_SetRenderDrawColor(app.renderer, 0, 0, 0, 0);

Expand All @@ -98,31 +99,31 @@ int init()
game.font_outline = TTF_OpenFont(EDITOR_FONT, EDITOR_FONT_SIZE);
TTF_SetFontOutline(game.font_outline, EDITOR_FONT_OUTLINE);

SDL_Color font_color = { 255, 255, 255, 230 };
SDL_Color font_outline_color = { 10, 10, 10, 200 };
SDL_Color font_color = { 255, 255, 255, 210 };
SDL_Color font_outline_color = { 10, 10, 10, 180 };
SDL_Surface* font_outline_surface;
SDL_Surface* font_surface;
SDL_Rect font_rect;

char map_tile_str[TILE_CHAR_LIMIT] = { 0 };
for (int tile_index = 0; tile_index < NUMBER_OF_TILES; tile_index++) {
for (int tile_index = 0; tile_index < TILE_ATTRIBUTES_LIMIT; tile_index++) {
snprintf(map_tile_str, sizeof(char[TILE_CHAR_LIMIT]), "%-3i", tile_index);
create_outlined_font(game, map_tile_str, font_outline_surface, font_surface, font_outline_color, font_color, font_rect);

game.editor.text_surfaces[tile_index] = font_outline_surface;
game.editor.text_textures[tile_index] = SDL_CreateTextureFromSurface(app.renderer, game.editor.text_surfaces[tile_index]);
game.editor.text_surfaces[tile_index] = *font_outline_surface;
game.editor.text_textures[tile_index] = SDL_CreateTextureFromSurface(app.renderer, &game.editor.text_surfaces[tile_index]);
}

// Set empty column label:
snprintf(map_tile_str, sizeof(char[TILE_CHAR_LIMIT]), "%-3i", EMPTY_COLUMN);
create_outlined_font(game, map_tile_str, font_outline_surface, font_surface, font_outline_color, font_color, font_rect);

if (EMPTY_COLUMN < 0) {
game.editor.text_surfaces[NUMBER_OF_TILES + EMPTY_COLUMN] = font_outline_surface;
game.editor.text_textures[NUMBER_OF_TILES + EMPTY_COLUMN] = SDL_CreateTextureFromSurface(app.renderer, game.editor.text_surfaces[NUMBER_OF_TILES + EMPTY_COLUMN]);
game.editor.text_surfaces[TILE_ATTRIBUTES_LIMIT + EMPTY_COLUMN] = *font_outline_surface;
game.editor.text_textures[TILE_ATTRIBUTES_LIMIT + EMPTY_COLUMN] = SDL_CreateTextureFromSurface(app.renderer, &game.editor.text_surfaces[TILE_ATTRIBUTES_LIMIT + EMPTY_COLUMN]);
} else {
game.editor.text_surfaces[EMPTY_COLUMN] = font_outline_surface;
game.editor.text_textures[EMPTY_COLUMN] = SDL_CreateTextureFromSurface(app.renderer, game.editor.text_surfaces[EMPTY_COLUMN]);
game.editor.text_surfaces[EMPTY_COLUMN] = *font_outline_surface;
game.editor.text_textures[EMPTY_COLUMN] = SDL_CreateTextureFromSurface(app.renderer, &game.editor.text_surfaces[EMPTY_COLUMN]);
}

SDL_FreeSurface(font_surface);
Expand All @@ -137,13 +138,8 @@ int init()

game.map.layout_file_base = MAP_LAYOUT_FILE_BASE;
game.map.layout_file = MAP_LAYOUT_FILE;
// TODO:
// Try to figure out why this was being set twice, the same
// goes for attributes_modified_time.
// game.map.layout_modified_time = read_file_time(game.map.layout_file);
game.map.layout_modified_time = 0;
game.map.attributes_file = MAP_ATTRIBUTES_FILE;
// game.map.attributes_modified_time = read_file_time(game.map.attributes_file);
game.map.attributes_modified_time = 0;

read_map_layout(&game);
Expand Down
Loading

0 comments on commit 58bd56d

Please sign in to comment.