aboutsummaryrefslogtreecommitdiff
path: root/src/sdl/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/sdl/main.c')
-rw-r--r--src/sdl/main.c75
1 files changed, 55 insertions, 20 deletions
diff --git a/src/sdl/main.c b/src/sdl/main.c
index 9341f4a..5eca49c 100644
--- a/src/sdl/main.c
+++ b/src/sdl/main.c
@@ -94,11 +94,40 @@ solve_on_error(
die("Error solving level: %s", err);
}
+static void
+set_level(
+ SDL_Window * const win,
+ draw_ctx_t * const draw_ctx,
+ sok_ctx_t * const ctx,
+ const size_t level_num
+) {
+ // get level data
+ draw_ctx->level = levels_get_level(level_num);
+
+ // load level
+ if (!sok_ctx_set_level(ctx, draw_ctx->level->data)) {
+ die("Couldn't load level %zu", level_num);
+ }
+
+ // build title
+ char buf[1024];
+ snprintf(
+ buf, sizeof(buf),
+ "Sokoban: %s: %s (#%zu)",
+ draw_ctx->level->pack,
+ draw_ctx->level->name,
+ level_num
+ );
+
+ // set window title
+ SDL_SetWindowTitle(win, buf);
+}
+
int main(int argc, char *argv[]) {
size_t level_num = (argc > 1) ? atoi(argv[1]) : 0,
zoom = 0;
- const level_t *level = levels_get_level(level_num);
const char *sprites_png_path = "../assets/sprites.png";
+ const char *font_path = "../assets/roboto.ttf";
// init warp buffer
warp_buf_t warp_buf;
@@ -108,10 +137,6 @@ int main(int argc, char *argv[]) {
sok_ctx_t ctx;
sok_ctx_init(&ctx, NULL);
- if (!sok_ctx_set_level(&ctx, level->data)) {
- die("Couldn't load level %zu", level_num);
- }
-
// init sdl
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO)) {
die("SDL_Init(): %s", SDL_GetError());
@@ -119,7 +144,18 @@ int main(int argc, char *argv[]) {
// register exit handler
if (atexit(SDL_Quit)) {
- die("atexit(): %s", strerror(errno));
+ die("atexit(SDL_Init): %s", strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+
+ // init SDL_TTF
+ if (TTF_Init()) {
+ die("TTF_Init(): %s", TTF_GetError());
+ }
+
+ // register exit handler
+ if (atexit(TTF_Quit)) {
+ die("atexit(TTF_Quit): %s", strerror(errno));
exit(EXIT_FAILURE);
}
@@ -135,15 +171,23 @@ int main(int argc, char *argv[]) {
// init draw context
draw_ctx_t draw_ctx = {
.level_num = &level_num,
- .level = level,
.ctx = &ctx,
.renderer = renderer,
.zoom = &zoom,
};
+ // set level
+ set_level(win, &draw_ctx, &ctx, level_num);
+
// init sprites
sprites_init(renderer, sprites_png_path, draw_ctx.sprites);
+ // load font
+ draw_ctx.font = TTF_OpenFontIndex(font_path, 16, 0);
+ if (!draw_ctx.font) {
+ die("TTF_OpenFontIndex(): %s", TTF_GetError());
+ }
+
bool done = false;
SDL_Event ev;
while (!done) {
@@ -176,13 +220,9 @@ int main(int argc, char *argv[]) {
if (sok_ctx_is_done(&ctx)) {
// advance level
level_num++;
- level = levels_get_level(level_num);
- draw_ctx.level = level;
- // load next level
- if (!sok_ctx_set_level(&ctx, level->data)) {
- die("Couldn't load level %zu", level_num);
- }
+ // set level
+ set_level(win, &draw_ctx, &ctx, level_num);
} else {
warn("cannot advance to next level");
}
@@ -190,20 +230,15 @@ int main(int argc, char *argv[]) {
break;
case ACTION_RESET:
// reset level
- if (!sok_ctx_set_level(&ctx, level->data)) {
+ if (!sok_ctx_set_level(&ctx, draw_ctx.level->data)) {
die("Couldn't load level %zu", level_num);
}
break;
case ACTION_WARP:
if (warp_buf_get(&warp_buf, &level_num)) {
- level = levels_get_level(level_num);
- draw_ctx.level = level;
-
// load level
- if (!sok_ctx_set_level(&ctx, level->data)) {
- die("Couldn't load level %zu", level_num);
- }
+ set_level(win, &draw_ctx, &ctx, level_num);
// clear warp buf
warp_buf_clear(&warp_buf);