clang-tidy support; clang-tidy and clang-format fixes

This commit is contained in:
Sebastian Krzyszkowiak 2017-09-10 21:35:14 +02:00
parent 0a4580a762
commit 2b1248ce14
17 changed files with 754 additions and 710 deletions

View file

@ -4,7 +4,7 @@
# ALLEGRO5_ACODEC_INCLUDE_DIR - the allrgo5 include directory
# ALLEGRO5_ACODEC_LIBRARIES - Link these to use allegro5
#
message(${ALLEGRO5_INCLUDE_DIR})
FIND_PATH(ALLEGRO5_ACODEC_INCLUDE_DIR allegro5/allegro_acodec.h HINTS ${ALLEGRO5_INCLUDE_DIR})
SET(ALLEGRO5_ACODEC_NAMES ${ALLEGRO5_ACODEC_NAMES} allegro_acodec allegro_acodec_static liballegro_acodec liballegro_acodec_static AllegroAcodec-5.2 allegro_acodec-debug)

View file

@ -6,7 +6,9 @@ if (NOT LIBSUPERDERPY_CONFIG_INCLUDED)
set(EMSCRIPTEN_TOTAL_MEMORY "32" CACHE STRING "Amount of memory allocated by Emscripten (MB, must be multiple of 16)" )
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -std=c11")
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED true)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O1 -fno-optimize-sibling-calls -fno-omit-frame-pointer -fsanitize=leak -DLEAK_SANITIZER=1 -fno-common -fsanitize-recover=all")
if(APPLE)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-undefined,error")
@ -14,6 +16,17 @@ if (NOT LIBSUPERDERPY_CONFIG_INCLUDED)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--no-undefined")
endif(APPLE)
find_program(
CLANG_TIDY_EXE
NAMES "clang-tidy"
DOC "Path to clang-tidy executable"
)
if(NOT CLANG_TIDY_EXE)
message(STATUS "clang-tidy not found.")
else()
set(CMAKE_C_CLANG_TIDY "${CLANG_TIDY_EXE}" "-checks=*,-clang-analyzer-alpha.*,-google-readability-todo,-performance-type-promotion-in-math-fn,-misc-unused-parameters,-cert-msc30-c,-cert-msc50-cpp")
endif()
if(APPLE)
if(CMAKE_INSTALL_PREFIX MATCHES "/usr/local")
set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}")

View file

@ -18,12 +18,12 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "internal.h"
#include "utils.h"
#include <allegro5/allegro_primitives.h>
#include <allegro5/allegro_ttf.h>
#include <stdio.h>
#include <string.h>
#include "internal.h"
#include "utils.h"
SYMBOL_EXPORT void SelectSpritesheet(struct Game* game, struct Character* character, char* name) {
struct Spritesheet* tmp = character->spritesheets;
@ -35,7 +35,9 @@ SYMBOL_EXPORT void SelectSpritesheet(struct Game *game, struct Character *charac
while (tmp) {
if (!strcmp(tmp->name, name)) {
character->spritesheet = tmp;
if (character->successor) free(character->successor);
if (character->successor) {
free(character->successor);
}
if (tmp->successor) {
character->successor = strdup(tmp->successor);
} else {
@ -57,11 +59,12 @@ SYMBOL_EXPORT void SelectSpritesheet(struct Game *game, struct Character *charac
tmp = tmp->next;
}
PrintConsole(game, "ERROR: No spritesheets registered for %s with given name: %s", character->name, name);
return;
}
SYMBOL_EXPORT void ChangeSpritesheet(struct Game* game, struct Character* character, char* name) {
if (character->successor) free(character->successor);
if (character->successor) {
free(character->successor);
}
character->successor = strdup(name);
}
@ -84,7 +87,9 @@ SYMBOL_EXPORT void UnloadSpritesheets(struct Game *game, struct Character *chara
PrintConsole(game, "Unloading spritesheets for character %s...", character->name);
struct Spritesheet* tmp = character->spritesheets;
while (tmp) {
if (tmp->bitmap) al_destroy_bitmap(tmp->bitmap);
if (tmp->bitmap) {
al_destroy_bitmap(tmp->bitmap);
}
tmp->bitmap = NULL;
tmp = tmp->next;
}
@ -106,19 +111,21 @@ SYMBOL_EXPORT void RegisterSpritesheet(struct Game *game, struct Character *char
s = malloc(sizeof(struct Spritesheet));
s->name = strdup(name);
s->bitmap = NULL;
s->cols = atoi(al_get_config_value(config, "", "cols"));
s->rows = atoi(al_get_config_value(config, "", "rows"));
s->blanks = atoi(al_get_config_value(config, "", "blanks"));
s->delay = atof(al_get_config_value(config, "", "delay"));
s->cols = strtol(al_get_config_value(config, "", "cols"), NULL, 10);
s->rows = strtol(al_get_config_value(config, "", "rows"), NULL, 10);
s->blanks = strtol(al_get_config_value(config, "", "blanks"), NULL, 10);
s->delay = strtod(al_get_config_value(config, "", "delay"), NULL);
const char* val = al_get_config_value(config, "", "repeat");
if (val) {
s->repeat = atof(val);
s->repeat = strtod(val, NULL);
} else {
s->repeat = 0;
}
s->kill = false;
const char* kill = al_get_config_value(config, "", "kill");
if (kill) s->kill = atoi(kill);
if (kill) {
s->kill = strtol(kill, NULL, 10);
}
s->successor = NULL;
const char* successor = al_get_config_value(config, "", "successor");
if (successor) {
@ -157,21 +164,29 @@ SYMBOL_EXPORT void DestroyCharacter(struct Game *game, struct Character *charact
while (s) {
tmp = s;
s = s->next;
if (tmp->bitmap) al_destroy_bitmap(tmp->bitmap);
if (tmp->successor) free(tmp->successor);
if (tmp->bitmap) {
al_destroy_bitmap(tmp->bitmap);
}
if (tmp->successor) {
free(tmp->successor);
}
free(tmp->name);
free(tmp);
}
}
if (character->bitmap) al_destroy_bitmap(character->bitmap);
if (character->successor) free(character->successor);
if (character->bitmap) {
al_destroy_bitmap(character->bitmap);
}
if (character->successor) {
free(character->successor);
}
free(character->name);
free(character);
}
SYMBOL_EXPORT void AnimateCharacter(struct Game* game, struct Character* character, float speed_modifier) {
if (character->dead) return;
if (character->dead) { return; }
if (speed_modifier) {
character->pos_tmp++;
if (character->pos_tmp >= character->spritesheet->delay / speed_modifier) {
@ -198,14 +213,14 @@ SYMBOL_EXPORT void MoveCharacter(struct Game *game, struct Character *character,
}
SYMBOL_EXPORT void MoveCharacterF(struct Game* game, struct Character* character, float x, float y, float angle) {
if (character->dead) return;
if (character->dead) { return; }
character->x += x;
character->y += y;
character->angle += angle;
}
SYMBOL_EXPORT void SetCharacterPositionF(struct Game* game, struct Character* character, float x, float y, float angle) {
if (character->dead) return;
if (character->dead) { return; }
character->x = x;
character->y = y;
character->angle = angle;
@ -215,9 +230,8 @@ SYMBOL_EXPORT void SetCharacterPosition(struct Game *game, struct Character *cha
SetCharacterPositionF(game, character, x / (float)game->viewport.width, y / (float)game->viewport.height, angle);
}
SYMBOL_EXPORT void DrawScaledCharacterF(struct Game* game, struct Character* character, ALLEGRO_COLOR tint, float scalex, float scaley, int flags) {
if (character->dead) return;
if (character->dead) { return; }
int spritesheetX = al_get_bitmap_width(character->bitmap) * (character->pos % character->spritesheet->cols);
int spritesheetY = al_get_bitmap_height(character->bitmap) * (character->pos / character->spritesheet->cols);
al_draw_tinted_scaled_rotated_bitmap_region(character->spritesheet->bitmap, spritesheetX, spritesheetY, al_get_bitmap_width(character->bitmap), al_get_bitmap_height(character->bitmap), tint, al_get_bitmap_width(character->bitmap) / 2, al_get_bitmap_height(character->bitmap) / 2, character->x * game->viewport.width + al_get_bitmap_width(character->bitmap) * scalex / 2, character->y * game->viewport.height + al_get_bitmap_height(character->bitmap) * scaley / 2, scalex, scaley, character->angle, flags);
@ -228,7 +242,7 @@ SYMBOL_EXPORT void DrawCharacterF(struct Game *game, struct Character *character
}
SYMBOL_EXPORT void DrawScaledCharacter(struct Game* game, struct Character* character, ALLEGRO_COLOR tint, float scalex, float scaley, int flags) {
if (character->dead) return;
if (character->dead) { return; }
int spritesheetX = al_get_bitmap_width(character->bitmap) * (character->pos % character->spritesheet->cols);
int spritesheetY = al_get_bitmap_height(character->bitmap) * (character->pos / character->spritesheet->cols);
al_draw_tinted_scaled_rotated_bitmap_region(character->spritesheet->bitmap, spritesheetX, spritesheetY, al_get_bitmap_width(character->bitmap), al_get_bitmap_height(character->bitmap), tint, al_get_bitmap_width(character->bitmap) / 2, al_get_bitmap_height(character->bitmap) / 2, (int)(character->x * game->viewport.width + al_get_bitmap_width(character->bitmap) * scalex / 2), (int)(character->y * game->viewport.height + al_get_bitmap_height(character->bitmap) * scaley / 2), scalex, scaley, character->angle, flags);

View file

@ -21,9 +21,9 @@
#ifndef LIBSUPERDERPY_CHARACTER_H
#define LIBSUPERDERPY_CHARACTER_H
#include "libsuperderpy.h"
#include <allegro5/allegro.h>
#include <allegro5/allegro_font.h>
#include "libsuperderpy.h"
/*! \brief Structure representing one spritesheet for character animation. */
struct Spritesheet {
@ -64,10 +64,10 @@ void SelectSpritesheet(struct Game *game, struct Character *character, char* nam
void ChangeSpritesheet(struct Game* game, struct Character* character, char* name);
void RegisterSpritesheet(struct Game* game, struct Character* character, char* name);
void DrawCharacterF(struct Game *game, struct Character *character, ALLEGRO_COLOR tilt, int flags);
void DrawCharacter(struct Game *game, struct Character *character, ALLEGRO_COLOR tilt, int flags);
void DrawScaledCharacterF(struct Game *game, struct Character *character, ALLEGRO_COLOR tilt, float scalex, float scaley, int flags);
void DrawScaledCharacter(struct Game *game, struct Character *character, ALLEGRO_COLOR tilt, float scalex, float scaley, int flags);
void DrawCharacterF(struct Game* game, struct Character* character, ALLEGRO_COLOR tint, int flags);
void DrawCharacter(struct Game* game, struct Character* character, ALLEGRO_COLOR tint, int flags);
void DrawScaledCharacterF(struct Game* game, struct Character* character, ALLEGRO_COLOR tint, float scalex, float scaley, int flags);
void DrawScaledCharacter(struct Game* game, struct Character* character, ALLEGRO_COLOR tint, float scalex, float scaley, int flags);
struct Character* CreateCharacter(struct Game* game, char* name);
void DestroyCharacter(struct Game* game, struct Character* character);

View file

@ -17,9 +17,9 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <allegro5/allegro.h>
#include "internal.h"
#include "config.h"
#include "internal.h"
#include <allegro5/allegro.h>
SYMBOL_EXPORT void InitConfig(struct Game* game) {
const ALLEGRO_FILE_INTERFACE* interface = al_get_new_file_interface();
@ -28,7 +28,9 @@ SYMBOL_EXPORT void InitConfig(struct Game *game) {
ALLEGRO_PATH* data = al_create_path("SuperDerpy.ini");
al_join_paths(path, data);
game->_priv.config = al_load_config_file(al_path_cstr(path, ALLEGRO_NATIVE_PATH_SEP));
if (!game->_priv.config) game->_priv.config=al_create_config();
if (!game->_priv.config) {
game->_priv.config = al_create_config();
}
al_destroy_path(path);
al_destroy_path(data);
al_set_new_file_interface(interface);
@ -44,7 +46,10 @@ SYMBOL_EXPORT const char* GetConfigOption(struct Game *game, char* section, char
SYMBOL_EXPORT const char* GetConfigOptionDefault(struct Game* game, char* section, char* name, const char* def) {
const char* ret = GetConfigOption(game, section, name);
if (!ret) return def; else return ret;
if (!ret) {
return def;
}
return ret;
}
SYMBOL_EXPORT void DeinitConfig(struct Game* game) {

View file

@ -20,7 +20,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LIBSUPERDERPY_EMSCRIPTEN_H
#define LIBSUPERDERPY_EMSCRIPTEN_H
@ -68,7 +67,6 @@ ALLEGRO_EVENT_SOURCE *emscripten_get_audio_stream_event_source(ALLEGRO_AUDIO_STR
void emscripten_drain_audio_stream(ALLEGRO_AUDIO_STREAM* stream) __attribute__((unavailable("won't work in Emscripten until proper audio stream support is fixed!")));
ALLEGRO_AUDIO_STREAM* emscripten_create_audio_stream(size_t fragment_count, unsigned int frag_samples, unsigned int freq, ALLEGRO_AUDIO_DEPTH depth, ALLEGRO_CHANNEL_CONF chan_conf) __attribute__((unavailable("won't work in Emscripten until proper audio stream support is fixed!")));
#define al_create_audio_stream emscripten_create_audio_stream
#define al_load_audio_stream emscripten_load_audio_stream
#define al_load_audio_stream_f emscripten_load_audio_stream_f

View file

@ -18,9 +18,9 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "gamestate.h"
#include "internal.h"
#include "utils.h"
#include "gamestate.h"
static struct Gamestate* AddNewGamestate(struct Game* game, const char* name) {
struct Gamestate* tmp = game->_priv.gamestates;
@ -90,7 +90,7 @@ SYMBOL_EXPORT void UnloadGamestate(struct Game *game, const char* name) {
PrintConsole(game, "Gamestate \"%s\" already unloaded.", name);
return;
}
if (gs->started) gs->pending_stop=true;
if (gs->started) { gs->pending_stop = true; }
gs->pending_unload = true;
PrintConsole(game, "Gamestate \"%s\" marked to be UNLOADED.", name);
} else {

View file

@ -20,10 +20,9 @@
#ifndef LIBSUPERDERPY_GAMESTATE_H
#define LIBSUPERDERPY_GAMESTATE_H
#include "libsuperderpy.h"
#include <allegro5/allegro.h>
struct Game;
struct Gamestate_API {
void (*Gamestate_Draw)(struct Game* game, void* data);
void (*Gamestate_Logic)(struct Game* game, void* data);

View file

@ -17,12 +17,12 @@
* Also, ponies.
*/
#include <stdio.h>
#include <dlfcn.h>
#include <allegro5/allegro_ttf.h>
#include "internal.h"
#include "libsuperderpy.h"
#include "3rdparty/valgrind.h"
#include "libsuperderpy.h"
#include <allegro5/allegro_ttf.h>
#include <dlfcn.h>
#include <stdio.h>
SYMBOL_INTERNAL void DrawGamestates(struct Game* game) {
ClearScreen(game);
@ -183,7 +183,9 @@ SYMBOL_INTERNAL void GamestateProgress(struct Game *game) {
float progressCount = *(tmp->api->Gamestate_ProgressCount) ? (float)*(tmp->api->Gamestate_ProgressCount) : 1;
float progress = ((game->_priv.loading.progress / progressCount) / (float)game->_priv.loading.toLoad) + (game->_priv.loading.loaded / (float)game->_priv.loading.toLoad);
game->loading_progress = progress;
if (game->config.debug) PrintConsole(game, "[%s] Progress: %d% (%d/%d)", tmp->name, (int)(progress*100), game->_priv.loading.progress, *(tmp->api->Gamestate_ProgressCount));
if (game->config.debug) {
PrintConsole(game, "[%s] Progress: %d% (%d/%d)", tmp->name, (int)(progress * 100), game->_priv.loading.progress, *(tmp->api->Gamestate_ProgressCount));
}
#ifdef LIBSUPERDERPY_SINGLE_THREAD
DrawGamestates(game);
if (tmp->showLoading) (*game->_priv.loading.gamestate->api->Gamestate_Draw)(game, game->_priv.loading.gamestate->data);
@ -207,7 +209,8 @@ SYMBOL_INTERNAL bool OpenGamestate(struct Game *game, struct Gamestate *gamestat
SYMBOL_INTERNAL bool LinkGamestate(struct Game* game, struct Gamestate* gamestate) {
gamestate->api = malloc(sizeof(struct Gamestate_API));
#define GS_ERROR FatalError(game, false, "Error on resolving gamestate's %s symbol: %s", gamestate->name, dlerror()); /* TODO: move out */ \
#define GS_ERROR \
FatalError(game, false, "Error on resolving gamestate's %s symbol: %s", gamestate->name, dlerror()); /* TODO: move out */ \
free(gamestate->api); \
return false;
@ -285,14 +288,13 @@ SYMBOL_INTERNAL struct libsuperderpy_list* RemoveFromList(struct libsuperderpy_l
d = tmp->data;
free(tmp);
return d;
} else {
}
start = tmp->next;
d = tmp->data;
free(tmp);
*list = start;
return d;
}
}
prev = tmp;
tmp = tmp->next;
}
@ -351,12 +353,10 @@ SYMBOL_INTERNAL void ClearScreen(struct Game *game) {
}
static void DrawQueue(struct Game* game, struct TM_Action* queue, int clipX, int clipY) {
int pos = clipX;
struct TM_Action* pom = queue;
while (pom != NULL) {
int width = al_get_text_width(game->_priv.font_console, pom->name);
al_draw_filled_rectangle(pos - (10 / 3200.0) * al_get_display_width(game->display), clipY, pos + width + (10 / 3200.0) * al_get_display_width(game->display), clipY + (60 / 1800.0) * al_get_display_height(game->display), pom->active ? al_map_rgba(255, 255, 255, 192) : al_map_rgba(0, 0, 0, 0));
al_draw_rectangle(pos - (10 / 3200.0) * al_get_display_width(game->display), clipY, pos + width + (10 / 3200.0) * al_get_display_width(game->display), clipY + (60 / 1800.0) * al_get_display_height(game->display), al_map_rgb(255, 255, 255), 2);

View file

@ -18,9 +18,9 @@
#ifndef LIBSUPERDERPY_INTERNAL_H
#define LIBSUPERDERPY_INTERNAL_H
#include "libsuperderpy.h"
#include <allegro5/allegro.h>
#include <allegro5/allegro_font.h>
#include "libsuperderpy.h"
#if defined(_WIN32) || defined(__CYGWIN__)
#define SYMBOL_INTERNAL

View file

@ -25,21 +25,20 @@
#define ALLEGRO_UNSTABLE
#endif
#include <stdio.h>
#include <math.h>
#include <locale.h>
#include <dlfcn.h>
#include <unistd.h>
#include <libgen.h>
#include <sys/param.h>
#include "internal.h"
#include "libsuperderpy.h"
#include "internal.h"
#include <dlfcn.h>
#include <libgen.h>
#include <locale.h>
#include <math.h>
#include <stdio.h>
#include <sys/param.h>
#include <unistd.h>
#ifdef ALLEGRO_MACOSX
#include <mach-o/dyld.h>
#endif
SYMBOL_EXPORT struct Game* libsuperderpy_init(int argc, char** argv, const char* name, struct Viewport viewport) {
struct Game* game = calloc(1, sizeof(struct Game));
game->name = name;
@ -53,7 +52,6 @@ SYMBOL_EXPORT struct Game* libsuperderpy_init(int argc, char** argv, const char*
chdir(dirname(exe_path));
#endif
if (!al_init()) {
fprintf(stderr, "failed to initialize allegro!\n");
free(game);
@ -79,15 +77,15 @@ SYMBOL_EXPORT struct Game* libsuperderpy_init(int argc, char** argv, const char*
game->handlers.event = NULL;
game->handlers.destroy = NULL;
game->config.fullscreen = atoi(GetConfigOptionDefault(game, "SuperDerpy", "fullscreen", "1"));
game->config.music = atoi(GetConfigOptionDefault(game, "SuperDerpy", "music", "10"));
game->config.voice = atoi(GetConfigOptionDefault(game, "SuperDerpy", "voice", "10"));
game->config.fx = atoi(GetConfigOptionDefault(game, "SuperDerpy", "fx", "10"));
game->config.debug = atoi(GetConfigOptionDefault(game, "SuperDerpy", "debug", "0"));
game->config.width = atoi(GetConfigOptionDefault(game, "SuperDerpy", "width", "1280"));
if (game->config.width<320) game->config.width=320;
game->config.height = atoi(GetConfigOptionDefault(game, "SuperDerpy", "height", "720"));
if (game->config.height<180) game->config.height=180;
game->config.fullscreen = strtol(GetConfigOptionDefault(game, "SuperDerpy", "fullscreen", "1"), NULL, 10);
game->config.music = strtol(GetConfigOptionDefault(game, "SuperDerpy", "music", "10"), NULL, 10);
game->config.voice = strtol(GetConfigOptionDefault(game, "SuperDerpy", "voice", "10"), NULL, 10);
game->config.fx = strtol(GetConfigOptionDefault(game, "SuperDerpy", "fx", "10"), NULL, 10);
game->config.debug = strtol(GetConfigOptionDefault(game, "SuperDerpy", "debug", "0"), NULL, 10);
game->config.width = strtol(GetConfigOptionDefault(game, "SuperDerpy", "width", "1280"), NULL, 10);
if (game->config.width < 320) { game->config.width = 320; }
game->config.height = strtol(GetConfigOptionDefault(game, "SuperDerpy", "height", "720"), NULL, 10);
if (game->config.height < 180) { game->config.height = 180; }
game->_priv.showconsole = game->config.debug;
game->_priv.showtimeline = false;
@ -133,7 +131,7 @@ SYMBOL_EXPORT struct Game* libsuperderpy_init(int argc, char** argv, const char*
game->touch = false;
if (!atoi(GetConfigOptionDefault(game, "SuperDerpy", "disableTouch", "0"))) {
if (!strtol(GetConfigOptionDefault(game, "SuperDerpy", "disableTouch", "0"), NULL, 10)) {
game->touch = al_install_touch_input();
}
@ -149,7 +147,7 @@ SYMBOL_EXPORT struct Game* libsuperderpy_init(int argc, char** argv, const char*
#ifdef __EMSCRIPTEN__
al_set_new_display_flags((al_get_new_display_flags() | ALLEGRO_WINDOWED) ^ ALLEGRO_FULLSCREEN_WINDOW);
#endif
al_set_new_display_option(ALLEGRO_VSYNC, 2-atoi(GetConfigOptionDefault(game, "SuperDerpy", "vsync", "1")), ALLEGRO_SUGGEST);
al_set_new_display_option(ALLEGRO_VSYNC, 2 - strtol(GetConfigOptionDefault(game, "SuperDerpy", "vsync", "1"), NULL, 10), ALLEGRO_SUGGEST);
al_set_new_display_option(ALLEGRO_OPENGL, true, ALLEGRO_REQUIRE);
#ifdef LIBSUPERDERPY_ORIENTATION_LANDSCAPE
@ -182,7 +180,7 @@ SYMBOL_EXPORT struct Game* libsuperderpy_init(int argc, char** argv, const char*
al_set_display_icon(game->display, icon);
al_destroy_bitmap(icon);
if (game->config.fullscreen) al_hide_mouse_cursor(game->display);
if (game->config.fullscreen) { al_hide_mouse_cursor(game->display); }
al_inhibit_screensaver(true);
al_add_new_bitmap_flag(ALLEGRO_MIN_LINEAR | ALLEGRO_MAG_LINEAR);
@ -306,7 +304,6 @@ SYMBOL_INTERNAL void libsuperderpy_mainloop(void *g) {
// TODO: split mainloop to functions to make it readable
ALLEGRO_EVENT ev;
if (game->_priv.draw && ((redraw && al_is_event_queue_empty(game->_priv.event_queue)) || (game->_priv.gamestate_scheduled))) {
game->_priv.gamestate_scheduled = false;
struct Gamestate* tmp = game->_priv.gamestates;
@ -324,7 +321,7 @@ SYMBOL_INTERNAL void libsuperderpy_mainloop(void *g) {
tmp->pending_stop = false;
}
if (tmp->pending_load) game->_priv.loading.toLoad++;
if (tmp->pending_load) { game->_priv.loading.toLoad++; }
tmp = tmp->next;
}
@ -342,7 +339,9 @@ SYMBOL_INTERNAL void libsuperderpy_mainloop(void *g) {
}
if (tmp->pending_load) {
al_stop_timer(game->_priv.timer);
if (tmp->showLoading) (*game->_priv.loading.gamestate->api->Gamestate_Start)(game, game->_priv.loading.gamestate->data);
if (tmp->showLoading) {
(*game->_priv.loading.gamestate->api->Gamestate_Start)(game, game->_priv.loading.gamestate->data);
}
if (!tmp->api) {
if (!OpenGamestate(game, tmp) || !LinkGamestate(game, tmp)) {
@ -366,7 +365,9 @@ SYMBOL_INTERNAL void libsuperderpy_mainloop(void *g) {
al_run_detached_thread(GamestateLoadingThread, &data);
while (game->_priv.loading.inProgress) {
DrawGamestates(game);
if (tmp->showLoading) (*game->_priv.loading.gamestate->api->Gamestate_Draw)(game, game->_priv.loading.gamestate->data);
if (tmp->showLoading) {
(*game->_priv.loading.gamestate->api->Gamestate_Draw)(game, game->_priv.loading.gamestate->data);
}
DrawConsole(game);
al_flip_display();
}
@ -381,9 +382,10 @@ SYMBOL_INTERNAL void libsuperderpy_mainloop(void *g) {
tmp->loaded = true;
tmp->pending_load = false;
}
if (tmp->showLoading) (*game->_priv.loading.gamestate->api->Gamestate_Stop)(game, game->_priv.loading.gamestate->data);
if (tmp->showLoading) {
(*game->_priv.loading.gamestate->api->Gamestate_Stop)(game, game->_priv.loading.gamestate->data);
}
al_resume_timer(game->_priv.timer);
}
@ -404,7 +406,9 @@ SYMBOL_INTERNAL void libsuperderpy_mainloop(void *g) {
al_resume_timer(game->_priv.timer);
}
if ((tmp->started) || (tmp->pending_start) || (tmp->pending_load)) gameActive = true;
if ((tmp->started) || (tmp->pending_start) || (tmp->pending_load)) {
gameActive = true;
}
tmp = tmp->next;
}
@ -424,7 +428,6 @@ SYMBOL_INTERNAL void libsuperderpy_mainloop(void *g) {
redraw = false;
} else {
#ifdef __EMSCRIPTEN__
if (al_is_event_queue_empty(game->_priv.event_queue)) {
return;
@ -442,22 +445,19 @@ SYMBOL_INTERNAL void libsuperderpy_mainloop(void *g) {
if ((ev.type == ALLEGRO_EVENT_TIMER) && (ev.timer.source == game->_priv.timer)) {
LogicGamestates(game);
redraw = true;
}
else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
} else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
#ifdef __EMSCRIPTEN__
libsuperderpy_mainloop_exit(game);
#endif
break;
}
else if(ev.type == ALLEGRO_EVENT_DISPLAY_HALT_DRAWING) {
} else if (ev.type == ALLEGRO_EVENT_DISPLAY_HALT_DRAWING) {
PrintConsole(game, "Engine halted.");
game->_priv.draw = false;
al_stop_timer(game->_priv.timer);
al_detach_voice(game->audio.v);
FreezeGamestates(game);
al_acknowledge_drawing_halt(game->display);
}
else if(ev.type == ALLEGRO_EVENT_DISPLAY_RESUME_DRAWING) {
} else if (ev.type == ALLEGRO_EVENT_DISPLAY_RESUME_DRAWING) {
game->_priv.draw = true;
al_acknowledge_drawing_resume(game->display);
PrintConsole(game, "Engine resumed.");
@ -465,8 +465,7 @@ SYMBOL_INTERNAL void libsuperderpy_mainloop(void *g) {
UnfreezeGamestates(game);
al_attach_mixer_to_voice(game->audio.mixer, game->audio.v);
al_resume_timer(game->_priv.timer);
}
else if(ev.type == ALLEGRO_EVENT_DISPLAY_RESIZE) {
} else if (ev.type == ALLEGRO_EVENT_DISPLAY_RESIZE) {
al_acknowledge_resize(game->display);
SetupViewport(game, game->viewport_config);
}
@ -479,8 +478,7 @@ SYMBOL_INTERNAL void libsuperderpy_mainloop(void *g) {
if (ev.keyboard.modifiers & ALLEGRO_KEYMOD_CTRL) {
game->_priv.showtimeline = game->_priv.showconsole;
}
}
else if ((ev.type == ALLEGRO_EVENT_KEY_DOWN) && (game->config.debug) && (ev.keyboard.keycode == ALLEGRO_KEY_F1)) {
} else if ((ev.type == ALLEGRO_EVENT_KEY_DOWN) && (game->config.debug) && (ev.keyboard.keycode == ALLEGRO_KEY_F1)) {
int i;
for (i = 0; i < 512; i++) {
LogicGamestates(game);
@ -490,14 +488,14 @@ SYMBOL_INTERNAL void libsuperderpy_mainloop(void *g) {
} else if ((ev.type == ALLEGRO_EVENT_KEY_DOWN) && (game->config.debug) && (ev.keyboard.keycode == ALLEGRO_KEY_F10)) {
double speed = ALLEGRO_BPS_TO_SECS(al_get_timer_speed(game->_priv.timer)); // inverting
speed -= 10;
if (speed<10) speed = 10;
if (speed < 10) { speed = 10; }
al_set_timer_speed(game->_priv.timer, ALLEGRO_BPS_TO_SECS(speed));
game->_priv.showconsole = true;
PrintConsole(game, "DEBUG: Gameplay speed: %.2fx", speed / 60.0);
} else if ((ev.type == ALLEGRO_EVENT_KEY_DOWN) && (game->config.debug) && (ev.keyboard.keycode == ALLEGRO_KEY_F11)) {
double speed = ALLEGRO_BPS_TO_SECS(al_get_timer_speed(game->_priv.timer)); // inverting
speed += 10;
if (speed>600) speed = 600;
if (speed > 600) { speed = 600; }
al_set_timer_speed(game->_priv.timer, ALLEGRO_BPS_TO_SECS(speed));
game->_priv.showconsole = true;
PrintConsole(game, "DEBUG: Gameplay speed: %.2fx", speed / 60.0);

View file

@ -22,26 +22,28 @@
#ifndef LIBSUPERDERPY_MAIN_H
#define LIBSUPERDERPY_MAIN_H
struct Game;
#include <allegro5/allegro.h>
#include <allegro5/allegro_audio.h>
#include <allegro5/allegro_image.h>
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_primitives.h>
#include <allegro5/allegro_acodec.h>
#include <allegro5/allegro_audio.h>
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_image.h>
#include <allegro5/allegro_primitives.h>
#include <allegro5/allegro_ttf.h>
#ifdef ALLEGRO_ANDROID
#include <allegro5/allegro_android.h>
#endif
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#include "emscripten-audio-stream.h"
#include <emscripten.h>
#endif
#include <sys/param.h>
#include "gamestate.h"
#include "character.h"
#include "config.h"
#include "gamestate.h"
#include "timeline.h"
#include "utils.h"
#include "character.h"
#include <sys/param.h>
#ifndef LIBSUPERDERPY_DATA_TYPE
#define LIBSUPERDERPY_DATA_TYPE void
@ -142,7 +144,6 @@ struct Game {
} handlers;
LIBSUPERDERPY_DATA_TYPE* data;
};
struct Game* libsuperderpy_init(int argc, char** argv, const char* name, struct Viewport viewport);

View file

@ -17,10 +17,10 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <allegro5/allegro.h>
#include "timeline.h"
#include "internal.h"
#include "utils.h"
#include "timeline.h"
#include <allegro5/allegro.h>
SYMBOL_EXPORT struct Timeline* TM_Init(struct Game* game, char* name) {
PrintConsole(game, "Timeline Manager[%s]: init", name);
@ -121,8 +121,9 @@ SYMBOL_EXPORT void TM_Process(struct Timeline* timeline) {
} else {
pom = NULL;
}
} else {
pom = tmp->next;
}
else pom=tmp->next;
tmp = tmp2;
}
}
@ -133,7 +134,9 @@ static void PauseTimers(struct Timeline* timeline, bool pause) {
if (timeline->queue->timer) {
if (pause) {
al_stop_timer(timeline->queue->timer);
} else if (!timeline->queue->active) al_resume_timer(timeline->queue->timer);
} else if (!timeline->queue->active) {
al_resume_timer(timeline->queue->timer);
}
}
}
struct TM_Action* tmp = timeline->background;
@ -141,7 +144,9 @@ static void PauseTimers(struct Timeline* timeline, bool pause) {
if (tmp->timer) {
if (pause) {
al_stop_timer(tmp->timer);
} else if (!tmp->active) al_resume_timer(tmp->timer);
} else if (!tmp->active) {
al_resume_timer(tmp->timer);
}
}
tmp = tmp->next;
}
@ -182,7 +187,7 @@ SYMBOL_EXPORT void TM_Resume(struct Timeline* timeline) {
}
SYMBOL_EXPORT void TM_HandleEvent(struct Timeline* timeline, ALLEGRO_EVENT* ev) {
if (ev->type != ALLEGRO_EVENT_TIMER) return;
if (ev->type != ALLEGRO_EVENT_TIMER) { return; }
if (timeline->queue) {
if (ev->timer.source == timeline->queue->timer) {
timeline->queue->active = true;
@ -298,7 +303,7 @@ SYMBOL_EXPORT struct TM_Action* TM_AddQueuedBackgroundAction(struct Timeline* ti
TM_WrapArg(int, del, delay);
TM_WrapArg(bool, used, false);
struct TM_Arguments* arguments = TM_AddToArgs(NULL, 6, (void*)func, del, strdup(name), (void*)timeline, args, used);
return TM_AddAction(timeline, *runinbackground, arguments, "TM_BackgroundAction");
return TM_AddAction(timeline, runinbackground, arguments, "TM_BackgroundAction");
}
SYMBOL_EXPORT void TM_AddDelay(struct Timeline* timeline, int delay) {
@ -317,7 +322,9 @@ SYMBOL_EXPORT void TM_CleanQueue(struct Timeline* timeline) {
PrintConsole(timeline->game, "Timeline Manager[%s]: cleaning queue", timeline->name);
struct TM_Action *tmp, *pom = timeline->queue;
while (pom != NULL) {
if (*pom->function) (*pom->function)(timeline->game, pom, TM_ACTIONSTATE_DESTROY);
if (*pom->function) {
(*pom->function)(timeline->game, pom, TM_ACTIONSTATE_DESTROY);
}
if (pom->timer) {
al_stop_timer(pom->timer);
al_destroy_timer(pom->timer);
@ -335,7 +342,9 @@ SYMBOL_EXPORT void TM_CleanBackgroundQueue(struct Timeline* timeline) {
PrintConsole(timeline->game, "Timeline Manager[%s]: cleaning background queue", timeline->name);
struct TM_Action *tmp, *pom = timeline->background;
while (pom != NULL) {
if (*pom->function) (*pom->function)(timeline->game, pom, TM_ACTIONSTATE_DESTROY);
if (*pom->function) {
(*pom->function)(timeline->game, pom, TM_ACTIONSTATE_DESTROY);
}
if (pom->timer) {
al_stop_timer(pom->timer);
al_destroy_timer(pom->timer);
@ -374,7 +383,6 @@ SYMBOL_EXPORT void TM_Destroy(struct Timeline* timeline) {
}
SYMBOL_EXPORT struct TM_Arguments* TM_AddToArgs(struct TM_Arguments* args, int num, ...) {
va_list ap;
int i;
va_start(ap, num);

View file

@ -23,7 +23,9 @@
#include "libsuperderpy.h"
#define TM_WrapArg(type, result, val) type* result = malloc(sizeof(type)); *result = val;
#define TM_WrapArg(type, result, val) \
type* result = malloc(sizeof(type)); \
*result = val;
/*! \brief State of the TM_Action. */
enum TM_ActionState {

View file

@ -18,14 +18,14 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "utils.h"
#include "config.h"
#include "internal.h"
#include <allegro5/allegro_primitives.h>
#include <allegro5/allegro_ttf.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "internal.h"
#include "config.h"
#include "utils.h"
SYMBOL_EXPORT void DrawVerticalGradientRect(float x, float y, float w, float h, ALLEGRO_COLOR top, ALLEGRO_COLOR bottom) {
ALLEGRO_VERTEX v[] = {
@ -63,10 +63,10 @@ SYMBOL_EXPORT int DrawWrappedText(ALLEGRO_FONT *font, ALLEGRO_COLOR color, float
int height = al_get_font_line_height(font) + 1;
// Setup our strings
strcpy(stext, text);
strcpy(temp, "");
strncpy(stext, text, 1024);
strncpy(temp, "", 1024);
for (int i = 0; i < 40; i += 1) {
strcpy(lines[i], "");
strncpy(lines[i], "", 1024);
}
//-------------------- Code Begins
@ -74,20 +74,20 @@ SYMBOL_EXPORT int DrawWrappedText(ALLEGRO_FONT *font, ALLEGRO_COLOR color, float
pch = strtok_r(stext, " ", &context); // Get the first word.
do {
strcpy(word, ""); // Truncate the string, to ensure there's no crazy stuff in there from memory.
sprintf(word,"%s ", pch);
strcat(temp, word); // Append the word to the end of TempLine
strncpy(word, "", 255); // Truncate the string, to ensure there's no crazy stuff in there from memory.
snprintf(word, 255, "%s ", pch);
strncat(temp, word, 255); // Append the word to the end of TempLine
// This code checks for the new line character.
if (strcmp(word, breakchar) == 0) {
if (strncmp(word, breakchar, 255) == 0) {
line += 1; // Move down a Line
strcpy(temp, ""); // Clear the tempstring
strncpy(temp, "", 1024); // Clear the tempstring
} else {
if (al_get_text_width(font, temp) >= (width)) { // Check if text is larger than the area.
strcpy(temp, word); // clear the templine and add the word to it.
strncpy(temp, word, 255); // clear the templine and add the word to it.
line += 1; // Move to the next line.
}
if (line < 40) {
strcat(lines[line], word); // Append the word to whatever line we are currently on.
strncat(lines[line], word, 255); // Append the word to whatever line we are currently on.
}
}
pch = strtok_r(NULL, " ", &context); // Get the next word.
@ -158,7 +158,7 @@ SYMBOL_EXPORT void ScaleBitmap(ALLEGRO_BITMAP* source, int width, int height) {
}
SYMBOL_EXPORT ALLEGRO_BITMAP* LoadScaledBitmap(struct Game* game, char* filename, int width, int height) {
bool memoryscale = !atoi(GetConfigOptionDefault(game, "SuperDerpy", "GPU_scaling", "1"));
bool memoryscale = !strtol(GetConfigOptionDefault(game, "SuperDerpy", "GPU_scaling", "1"), NULL, 10);
ALLEGRO_BITMAP *source, *target = al_create_bitmap(width, height);
al_set_target_bitmap(target);
al_clear_to_color(al_map_rgba(0, 0, 0, 0));
@ -173,8 +173,7 @@ SYMBOL_EXPORT ALLEGRO_BITMAP* LoadScaledBitmap(struct Game *game, char* filename
if (memoryscale) {
al_set_new_bitmap_flags(flags);
ScaleBitmap(source, width, height);
}
else {
} else {
al_draw_scaled_bitmap(source, 0, 0, al_get_bitmap_width(source), al_get_bitmap_height(source), 0, 0, width, height, 0);
}
@ -182,10 +181,9 @@ SYMBOL_EXPORT ALLEGRO_BITMAP* LoadScaledBitmap(struct Game *game, char* filename
free(origfn);
return target;
}
SYMBOL_EXPORT void FatalError(struct Game *game, bool fatal, char* format, ...) {
SYMBOL_EXPORT void FatalError(struct Game* game, bool exit, char* format, ...) {
char text[1024] = {};
PrintConsole(game, "Fatal Error, displaying Blue Screen of Derp...");
va_list vl;
@ -209,7 +207,6 @@ SYMBOL_EXPORT void FatalError(struct Game *game, bool fatal, char* format, ...)
bool done = false;
while (!done) {
al_set_target_backbuffer(game->display);
al_clear_to_color(al_map_rgb(0, 0, 170));
@ -230,8 +227,7 @@ SYMBOL_EXPORT void FatalError(struct Game *game, bool fatal, char* format, ...)
al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), al_get_display_width(game->display) / 2 - al_get_text_width(game->_priv.font_bsod, header2) / 2, (int)(al_get_display_height(game->display) * 0.32 + 8 * al_get_font_line_height(game->_priv.font_bsod) * 1.25), ALLEGRO_ALIGN_LEFT, "* Press any key to destroy all muffins in the world.");
al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), al_get_display_width(game->display) / 2 - al_get_text_width(game->_priv.font_bsod, header2) / 2, (int)(al_get_display_height(game->display) * 0.32 + 9 * al_get_font_line_height(game->_priv.font_bsod) * 1.25), ALLEGRO_ALIGN_LEFT, "* Just kidding, please press any key anyway.");
if (fatal) {
if (exit) {
al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), al_get_display_width(game->display) / 2 - al_get_text_width(game->_priv.font_bsod, header2) / 2, (int)(al_get_display_height(game->display) * 0.32 + 11 * al_get_font_line_height(game->_priv.font_bsod) * 1.25), ALLEGRO_ALIGN_LEFT, "This is fatal error. My bad.");
al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), al_get_display_width(game->display) / 2, (int)(al_get_display_height(game->display) * 0.32 + 13 * al_get_font_line_height(game->_priv.font_bsod) * 1.25), ALLEGRO_ALIGN_CENTRE, "Press any key to quit _");
@ -263,7 +259,7 @@ SYMBOL_EXPORT void FatalError(struct Game *game, bool fatal, char* format, ...)
}
static void TestPath(char* filename, char* subpath, char** result) {
if (*result) return; //already found
if (*result) { return; } //already found
ALLEGRO_PATH* tail = al_create_path(filename);
ALLEGRO_PATH* path = al_get_standard_path(ALLEGRO_RESOURCES_PATH);
ALLEGRO_PATH* data = al_create_path(subpath);
@ -278,12 +274,18 @@ static void TestPath(char* filename, char* subpath, char** result) {
al_destroy_path(path);
}
#if defined(__clang__) || defined(__codemodel__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wformat-nonliteral"
#endif
SYMBOL_EXPORT char* GetGameName(struct Game* game, char* format) {
char* result = malloc(sizeof(char) * 255);
snprintf(result, 255, format, game->name);
return AddGarbage(game, result);
}
#if defined(__clang__) || defined(__codemodel__)
#pragma clang diagnostic pop
#endif
static char* TestDataFilePath(struct Game* game, char* filename) {
char* result = NULL;
@ -294,7 +296,7 @@ static char* TestDataFilePath(struct Game *game, char* filename) {
{
char origfn[255] = "data/";
strcat(origfn, filename);
strncat(origfn, filename, 249);
if (al_filename_exists(origfn)) {
return strdup(origfn);
@ -314,7 +316,6 @@ static char* TestDataFilePath(struct Game *game, char* filename) {
}
SYMBOL_EXPORT char* GetDataFilePath(struct Game* game, char* filename) {
char* result = 0;
#ifdef ALLEGRO_ANDROID
@ -357,6 +358,10 @@ SYMBOL_EXPORT char* GetDataFilePath(struct Game *game, char* filename) {
ALLEGRO_DEBUG_CHANNEL("libsuperderpy")
#if defined(__clang__) || defined(__codemodel__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wformat-nonliteral"
#endif
SYMBOL_EXPORT void PrintConsole(struct Game* game, char* format, ...) {
va_list vl;
va_start(vl, format);
@ -375,11 +380,12 @@ SYMBOL_EXPORT void PrintConsole(struct Game *game, char* format, ...) {
if (game->_priv.console_pos >= (sizeof(game->_priv.console) / sizeof(game->_priv.console[0]))) {
game->_priv.console_pos = 0;
}
return;
}
#if defined(__clang__) || defined(__codemodel__)
#pragma clang diagnostic pop
#endif
SYMBOL_EXPORT void SetupViewport(struct Game* game, struct Viewport config) {
game->viewport = config;
if ((game->viewport.width == 0) || (game->viewport.height == 0)) {
@ -387,7 +393,7 @@ SYMBOL_EXPORT void SetupViewport(struct Game *game, struct Viewport config) {
game->viewport.width = game->viewport.aspect * game->viewport.height;
if (game->viewport.width > al_get_display_width(game->display)) {
game->viewport.width = al_get_display_width(game->display);
game->viewport.height = game->viewport.width / (float)game->viewport.aspect;
game->viewport.height = game->viewport.width / game->viewport.aspect;
}
}
game->viewport.aspect = game->viewport.width / (float)game->viewport.height;
@ -407,21 +413,21 @@ SYMBOL_EXPORT void SetupViewport(struct Game *game, struct Viewport config) {
resolution = 1;
}
}
if ((!atoi(GetConfigOptionDefault(game, "SuperDerpy", "downscale", "1"))) && (resolution < 1)) {
if ((!strtol(GetConfigOptionDefault(game, "SuperDerpy", "downscale", "1"), NULL, 10)) && (resolution < 1)) {
resolution = 1;
}
if (!atoi(GetConfigOptionDefault(game, "SuperDerpy", "scaling", "1"))) {
if (!strtol(GetConfigOptionDefault(game, "SuperDerpy", "scaling", "1"), NULL, 10)) {
resolution = 1;
}
int clipWidth = game->viewport.width * resolution;
int clipHeight = game->viewport.height * resolution;
if (atoi(GetConfigOptionDefault(game, "SuperDerpy", "letterbox", "1"))) {
if (strtol(GetConfigOptionDefault(game, "SuperDerpy", "letterbox", "1"), NULL, 10)) {
int clipX = (al_get_display_width(game->display) - clipWidth) / 2;
int clipY = (al_get_display_height(game->display) - clipHeight) / 2;
al_build_transform(&game->projection, clipX, clipY, resolution, resolution, 0.0f);
al_set_clipping_rectangle(clipX, clipY, clipWidth, clipHeight);
} else if (atoi(GetConfigOptionDefault(game, "SuperDerpy", "scaling", "1"))) {
} else if (strtol(GetConfigOptionDefault(game, "SuperDerpy", "scaling", "1"), NULL, 10)) {
al_build_transform(&game->projection, 0, 0, al_get_display_width(game->display) / (float)game->viewport.width, al_get_display_height(game->display) / (float)game->viewport.height, 0.0f);
}
al_use_transform(&game->projection);

View file

@ -21,9 +21,9 @@
#ifndef LIBSUPERDERPY_UTILS_H
#define LIBSUPERDERPY_UTILS_H
#include "libsuperderpy.h"
#include <allegro5/allegro.h>
#include <allegro5/allegro_font.h>
#include "libsuperderpy.h"
#ifdef ALLEGRO_WINDOWS
#define LIBRARY_EXTENSION ".dll"