Skip to content

Commit

Permalink
Refactored SDL & message code (SDL1)
Browse files Browse the repository at this point in the history
Added sys.c
Renamed sys_sdl2.c to sys_sdl2.h
Moved message code to message.h
  • Loading branch information
twojstaryzdomu committed Jul 11, 2022
2 parents 4493d52 + 81e6aa7 commit 72444bf
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 95 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ endif

all: blues bbja pre2

blues: main.o sys_sdl2.o util.o $(BB_SRCS:.c=.o)
blues: main.o sys.o util.o $(BB_SRCS:.c=.o)
$(CC) $(LDFLAGS) -o $@ $^ $(SDL_LIBS) $(MODPLUG_LIBS)

bbja: main.o sys_sdl2.o util.o $(JA_SRCS:.c=.o)
bbja: main.o sys.o util.o $(JA_SRCS:.c=.o)
$(CC) $(LDFLAGS) -o $@ $^ $(SDL_LIBS) $(MODPLUG_LIBS)

pre2: main.o sys_sdl2.o util.o $(P2_SRCS:.c=.o)
pre2: main.o sys.o util.o $(P2_SRCS:.c=.o)
$(CC) $(LDFLAGS) -o $@ $^ $(SDL_LIBS) $(MODPLUG_LIBS)

clean:
Expand Down
49 changes: 49 additions & 0 deletions message.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

#ifndef MESSAGE_H__
#define MESSAGE_H__

#include <string.h>

#define MESSAGE_MAX 30
#define MESSAGE_TIMELIMIT 1000
#define MAX_MESSAGES 5

char *_queue[MAX_MESSAGES];

struct message_t {
void (*add)(char *m, ...);
char* (*get)();
void (*clear)(const char *m);
void (*clear_all)();
};

static void add(char *m, ...) {
int j;
for (int i = 0; i < MAX_MESSAGES; i++)
if (_queue[i] && strcmp(m, _queue[i]) == 0)
return;
for (int i = 0; i < MAX_MESSAGES; i++)
if (!_queue[i])
j = i;
_queue[ 0 + j ] = m;
}

static char* get() {
for (int i = 0; i < MAX_MESSAGES; i++)
if (_queue[i])
return _queue[i];
return 0;
}

static void clear(const char *m) {
for (int i = 0; i < MAX_MESSAGES; i++)
if (_queue[i] && strcmp(m, _queue[i]) == 0)
_queue[i] = 0;
}

static void clear_all() {
for (int i = 0; i < MAX_MESSAGES; i++)
_queue[i] = 0;
}

#endif
1 change: 1 addition & 0 deletions p2/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#define GAME_H__

#include "intern.h"
#include "message.h"
#include "sys.h"

extern struct options_t g_options;
Expand Down
16 changes: 8 additions & 8 deletions p2/level.c
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ void level_player_die() {
if ((g_options.cheats & CHEATS_UNLIMITED_LIFES) == 0) {
--g_vars.player_lifes;
g_vars.redraw_cache = true;
g_sys.add_message("Killed by death");
g_message.add("Killed by death");
g_vars.message.timelimit = 500;
g_vars.player_energy = 0;
}
Expand Down Expand Up @@ -3350,7 +3350,7 @@ static void level_cycle_palette() {
g_sys.palette_offset = 0;
sprintf(g_vars.message.s, "PALETTE %d", g_vars.palette);
g_vars.message.timelimit = 2500;
g_sys.add_message(g_vars.message.s);
g_message.add(g_vars.message.s);
}
if (g_vars.hybrid_color_flag != g_sys.hybrid_color)
g_vars.prev_palette = g_vars.palette;
Expand All @@ -3362,7 +3362,7 @@ static void level_cycle_palette() {
g_sys.cycle_palette = 0;
sprintf(g_vars.message.s, "PALETTE %d", g_vars.palette);
g_vars.message.timelimit = 500;
g_sys.add_message(g_vars.message.s);
g_message.add(g_vars.message.s);
}
g_sys.set_screen_palette(palette, 0, 16, 6);
}
Expand Down Expand Up @@ -3396,14 +3396,14 @@ static void level_update_light_palette() {
static void level_draw_messages() {
if (g_vars.message.timestamp) {
if (g_sys.get_timestamp() - g_vars.message.timestamp > (g_vars.message.timelimit ? g_vars.message.timelimit : MESSAGE_TIMELIMIT)) {
g_sys.clear_message(g_vars.message.s);
g_message.clear(g_vars.message.s);
memset(g_vars.message.s, 0, MESSAGE_MAX);
g_vars.message.timelimit = 0;
g_vars.message.timestamp = 0;
}
} else {
if (g_sys.get_message()) {
strncpy(g_vars.message.s, g_sys.get_message(), MESSAGE_MAX);
if (g_message.get()) {
strncpy(g_vars.message.s, g_message.get(), MESSAGE_MAX);
g_vars.message.timestamp = g_sys.get_timestamp();
}
}
Expand All @@ -3429,7 +3429,7 @@ static void level_resize() {
}
g_sys.update_screen(g_res.vga, 0);
sprintf(g_vars.message.s, "%dx%d", GAME_SCREEN_W, GAME_SCREEN_H);
g_sys.add_message(g_vars.message.s);
g_message.add(g_vars.message.s);
level_draw_messages();
}
}
Expand Down Expand Up @@ -4144,7 +4144,7 @@ void do_gameover_animation() {
void do_level() {
static const uint8_t music_tbl[] = { 9, 9, 0, 0, 0, 13, 4, 4, 10, 13, 16, 16, 16, 9, 14, 4 };
g_sys.render_set_sprites_clipping_rect(0, 0, TILEMAP_SCREEN_W, TILEMAP_SCREEN_H);
g_sys.clear_messages();
g_message.clear_all();
play_music(music_tbl[g_vars.level_num]);
load_level_data(g_vars.level_num);
set_level_palette();
Expand Down
41 changes: 41 additions & 0 deletions sys.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

#include "message.h"

struct message_t g_message = {
.add = add,
.get = get,
.clear = clear,
.clear_all = clear_all
};

#include "sys_sdl2.h"

struct sys_t g_sys = {
.init = sdl2_init,
.fini = sdl2_fini,
.set_screen_size = sdl2_set_screen_size,
.set_screen_palette = sdl2_set_screen_palette,
.set_palette_amiga = sdl2_set_palette_amiga,
.set_copper_bars = sdl2_set_copper_bars,
.set_palette_color = sdl2_set_palette_color,
.fade_in_palette = sdl2_fade_in_palette,
.fade_out_palette = sdl2_fade_out_palette,
.clear_slide = clear_slide,
.resize_screen = sdl2_resize_screen,
.update_screen = sdl2_update_screen,
.update_screen_cached = sdl2_update_screen_cached,
.shake_screen = sdl2_shake_screen,
.transition_screen = sdl2_transition_screen,
.process_events = sdl2_process_events,
.sleep = sdl2_sleep,
.get_timestamp = sdl2_get_timestamp,
.start_audio = sdl2_start_audio,
.stop_audio = sdl2_stop_audio,
.lock_audio = sdl2_lock_audio,
.unlock_audio = sdl2_unlock_audio,
.render_load_sprites = render_load_sprites,
.render_unload_sprites = render_unload_sprites,
.render_add_sprite = render_add_sprite,
.render_clear_sprites = render_clear_sprites,
.render_set_sprites_clipping_rect = render_set_sprites_clipping_rect
};
10 changes: 1 addition & 9 deletions sys.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@
#define GAME_SCREEN_W g_sys.w
#define GAME_SCREEN_H g_sys.h

#define MESSAGE_MAX 30
#define MESSAGE_TIMELIMIT 1000
#define MAX_MESSAGES 5

struct input_t {
uint8_t direction;
bool quit;
Expand Down Expand Up @@ -80,15 +76,10 @@ struct sys_t {
void (*render_add_sprite)(int spr_type, int frame, int x, int y, int xflip, bool centred);
void (*render_clear_sprites)();
void (*render_set_sprites_clipping_rect)(int x, int y, int w, int h);
void (*add_message)(char *m);
char* (*get_message)();
void (*clear_message)(const char *m);
void (*clear_messages)();
void (*clear_slide)();
struct sys_rect_t slide_rect;
enum sys_slide_e slide_type;
uint16_t slide_end;
char *message_queue[MAX_MESSAGES];
bool paused;
bool audio;
bool resize;
Expand All @@ -103,5 +94,6 @@ struct sys_t {
};

extern struct sys_t g_sys;
extern struct message_t g_message;

#endif /* SYS_H__ */
82 changes: 7 additions & 75 deletions sys_sdl2.c → sys_sdl2.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ static void sdl2_set_screen_size(int w, int h, const char *caption, int scale, b
_scale = MAX(_scale - 1, 1);
print_warning("Unable to fit %dx scaled %dx%d screen within %dx%d window bounds", scale, screen_w, screen_h, _window_w, _window_h, _scale);
sprintf(_s, "Unable to scale to %d", scale);
g_sys.add_message(_s);
g_message.add(_s);
return; // refuse to resize when not possible to fit window within bounds
}
if (!_size_lock) {
Expand Down Expand Up @@ -821,7 +821,7 @@ static void handle_keyevent(const SDL_keysym *keysym, bool keydown, struct input
g_sys.audio = !g_sys.audio;
SDL_PauseAudio(!g_sys.audio);
sprintf(_s, "Sound %s", g_sys.audio ? "on" : "off");
g_sys.add_message(_s);
g_message.add(_s);
}
break;
case SDLK_c:
Expand All @@ -843,15 +843,15 @@ static void handle_keyevent(const SDL_keysym *keysym, bool keydown, struct input
if (keydown) {
g_sys.animate_tiles = !g_sys.animate_tiles;
sprintf(_s, "Animated tiles %s", g_sys.animate_tiles ? "on" : "off");
g_sys.add_message(_s);
g_message.add(_s);
}
break;
case SDLK_h:
if (keydown) {
g_sys.hybrid_color = !g_sys.hybrid_color;
g_sys.cycle_palette = true;
sprintf(_s, "Hybrid colour %s", g_sys.hybrid_color ? "on" : "off");
g_sys.add_message(_s);
g_message.add(_s);
}
break;
case SDLK_i:
Expand All @@ -861,7 +861,7 @@ static void handle_keyevent(const SDL_keysym *keysym, bool keydown, struct input
case SDLK_j:
if (keydown) {
sprintf(_s, "Press jump button");
g_sys.add_message(_s);
g_message.add(_s);
_joystick_up_setup = true;
}
break;
Expand All @@ -886,7 +886,7 @@ static void handle_keyevent(const SDL_keysym *keysym, bool keydown, struct input
if (keydown) {
_size_lock = !_size_lock;
sprintf(_s, "Size %s", _size_lock ? "locked" : "unlocked");
g_sys.add_message(_s);
g_message.add(_s);
}
break;
case SDLK_t:
Expand Down Expand Up @@ -947,7 +947,7 @@ static void handle_joystickbutton(int button, int pressed, struct input_t *input
g_sys.input.jump_button = button;
_joystick_up_setup = false;
sprintf(_s, "Jump on %d key", g_sys.input.jump_button);
g_sys.add_message(_s);
g_message.add(_s);
return;
}
}
Expand Down Expand Up @@ -1165,71 +1165,3 @@ static void render_set_sprites_clipping_rect(int x, int y, int w, int h) {
_sprites_cliprect.w = w * _scale;
_sprites_cliprect.h = h * _scale;
}

static void add_message(char *m) {
int j;
for (int i = 0; i < MAX_MESSAGES; i++) {
if (g_sys.message_queue[i] && strcmp(m, g_sys.message_queue[i]) == 0)
return;
}
for (int i = 0; i < MAX_MESSAGES; i++) {
if (!g_sys.message_queue[i])
j = i;
}
g_sys.message_queue[ 0 + j ] = m;
}

static char* get_message() {
for (int i = 0; i < MAX_MESSAGES; i++) {
if (g_sys.message_queue[i])
return g_sys.message_queue[i];
}
return 0;
}

static void clear_message(const char *m) {
for (int i = 0; i < MAX_MESSAGES; i++) {
if (g_sys.message_queue[i] && strcmp(m, g_sys.message_queue[i]) == 0)
g_sys.message_queue[i] = 0;
}
}

static void clear_messages() {
for (int i = 0; i < MAX_MESSAGES; i++) {
g_sys.message_queue[i] = 0;
}
}

struct sys_t g_sys = {
.init = sdl2_init,
.fini = sdl2_fini,
.set_screen_size = sdl2_set_screen_size,
.set_screen_palette = sdl2_set_screen_palette,
.set_palette_amiga = sdl2_set_palette_amiga,
.set_copper_bars = sdl2_set_copper_bars,
.set_palette_color = sdl2_set_palette_color,
.fade_in_palette = sdl2_fade_in_palette,
.fade_out_palette = sdl2_fade_out_palette,
.clear_slide = clear_slide,
.resize_screen = sdl2_resize_screen,
.update_screen = sdl2_update_screen,
.update_screen_cached = sdl2_update_screen_cached,
.shake_screen = sdl2_shake_screen,
.transition_screen = sdl2_transition_screen,
.process_events = sdl2_process_events,
.sleep = sdl2_sleep,
.get_timestamp = sdl2_get_timestamp,
.start_audio = sdl2_start_audio,
.stop_audio = sdl2_stop_audio,
.lock_audio = sdl2_lock_audio,
.unlock_audio = sdl2_unlock_audio,
.render_load_sprites = render_load_sprites,
.render_unload_sprites = render_unload_sprites,
.render_add_sprite = render_add_sprite,
.render_clear_sprites = render_clear_sprites,
.render_set_sprites_clipping_rect = render_set_sprites_clipping_rect,
.add_message = add_message,
.get_message = get_message,
.clear_message = clear_message,
.clear_messages = clear_messages
};

0 comments on commit 72444bf

Please sign in to comment.