diff --git a/CMakeLists.txt b/CMakeLists.txt index de7fdd4..8d44740 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,7 +7,7 @@ set(SDL2PATH "${PROJECT_SOURCE_DIR}/dependencies/SDL2-2.0.16/x86_64-w64-mingw32" find_package(SDL2 REQUIRED) include_directories(${SDL2_INCLUDE_DIR}) -add_executable(izanami src/packages/game/main.c src/packages/game/math/IZ_vector.c src/packages/game/math/IZ_vector.h src/packages/game/math/IZ_math.c src/packages/game/math/IZ_math.h src/packages/game/core/IZ_placeable.c src/packages/game/core/IZ_placeable.h src/packages/game/core/IZ_movable.c src/packages/game/core/IZ_movable.h src/packages/game/core/IZ_spatial.c src/packages/game/core/IZ_spatial.h) +add_executable(izanami src/packages/game/main.c src/packages/game/math/IZ_vector.c src/packages/game/math/IZ_vector.h src/packages/game/math/IZ_math.c src/packages/game/math/IZ_math.h src/packages/game/core/IZ_placeable.c src/packages/game/core/IZ_placeable.h src/packages/game/core/IZ_movable.c src/packages/game/core/IZ_movable.h src/packages/game/core/IZ_spatial.c src/packages/game/core/IZ_spatial.h src/packages/game/input/IZ_action.c src/packages/game/input/IZ_action.h src/packages/game/config/IZ_config.c src/packages/game/config/IZ_config.h src/packages/game/config/IZ_config_window.c src/packages/game/config/IZ_config_window.h src/packages/game/config/IZ_config_mapping.c src/packages/game/config/IZ_config_mapping.h src/packages/game/config/IZ_config_pool.c src/packages/game/config/IZ_config_pool.h src/packages/game/core/IZ_constants.h) target_link_libraries(izanami ${SDL2_LIBRARY}) diff --git a/src/packages/game/config/IZ_config.c b/src/packages/game/config/IZ_config.c new file mode 100644 index 0000000..88ecfc7 --- /dev/null +++ b/src/packages/game/config/IZ_config.c @@ -0,0 +1 @@ +#include "IZ_config.h" diff --git a/src/packages/game/config/IZ_config.h b/src/packages/game/config/IZ_config.h new file mode 100644 index 0000000..014021f --- /dev/null +++ b/src/packages/game/config/IZ_config.h @@ -0,0 +1,49 @@ +#ifndef IZ_CONFIG_H +#define IZ_CONFIG_H + +#include "../core/IZ_constants.h" + +#include "IZ_config_mapping.h" +#include "IZ_config_pool.h" +#include "IZ_config_window.h" + +/** + * File path of the configuration file. + */ +static const char* IZ_CONFIG_FILE_PATH = "config.ini"; + +/** + * Structure that defines the application's internal configuration. + */ +typedef struct { + /** + * Window configuration. + */ + IZ_ConfigWindow window; + /** + * Pool configuration. + */ + IZ_ConfigPool pool; + /** + * Keyboard and joystick mappings for each controllable agent. + */ + IZ_ConfigMapping mapping[IZ_MAX_PLAYERS]; +} IZ_Config; + +/** + * Writes the configuration to a file. + * @param config The pointer to the configuration data. + * @param filename The filename of the destination file. + * @return Result code of the operation. + */ +unsigned int IZ_ConfigSave(IZ_Config* config, const char* filename); + +/** + * Reads the configuration from a file. + * @param config The pointer to the configuration data. + * @param filename The filename of the destination file. + * @return Error message, or NULL if there are no errors. + */ +unsigned int IZ_ConfigLoad(IZ_Config* config, const char* filename); + +#endif diff --git a/src/packages/game/config/IZ_config_mapping.c b/src/packages/game/config/IZ_config_mapping.c new file mode 100644 index 0000000..43234fc --- /dev/null +++ b/src/packages/game/config/IZ_config_mapping.c @@ -0,0 +1 @@ +#include "IZ_config_mapping.h" diff --git a/src/packages/game/config/IZ_config_mapping.h b/src/packages/game/config/IZ_config_mapping.h new file mode 100644 index 0000000..9bc83e5 --- /dev/null +++ b/src/packages/game/config/IZ_config_mapping.h @@ -0,0 +1,20 @@ +#ifndef IZ_CONFIG_MAPPING_H +#define IZ_CONFIG_MAPPING_H + +/** + * Structure that defines the application's input configuration. + */ +typedef struct { + /** + * Mapping of keyboard scan codes to each action. + */ + unsigned int keyboard[16]; + /** + * Mapping of joystick buttons to each action. + * + * Note: directional actions are not included here. + */ + unsigned int joystick[12]; +} IZ_ConfigMapping; + +#endif diff --git a/src/packages/game/config/IZ_config_pool.c b/src/packages/game/config/IZ_config_pool.c new file mode 100644 index 0000000..1ebc382 --- /dev/null +++ b/src/packages/game/config/IZ_config_pool.c @@ -0,0 +1 @@ +#include "IZ_config_pool.h" diff --git a/src/packages/game/config/IZ_config_pool.h b/src/packages/game/config/IZ_config_pool.h new file mode 100644 index 0000000..0497053 --- /dev/null +++ b/src/packages/game/config/IZ_config_pool.h @@ -0,0 +1,14 @@ +#ifndef IZ_CONFIG_POOL_H +#define IZ_CONFIG_POOL_H + +/** + * Structure that defines the application's pools configuration. + */ +typedef struct { + /** + * Capacity of the characters pool. + */ + unsigned int characters; +} IZ_ConfigPool; + +#endif diff --git a/src/packages/game/config/IZ_config_window.c b/src/packages/game/config/IZ_config_window.c new file mode 100644 index 0000000..0a76866 --- /dev/null +++ b/src/packages/game/config/IZ_config_window.c @@ -0,0 +1 @@ +#include "IZ_config_window.h" diff --git a/src/packages/game/config/IZ_config_window.h b/src/packages/game/config/IZ_config_window.h new file mode 100644 index 0000000..3961c13 --- /dev/null +++ b/src/packages/game/config/IZ_config_window.h @@ -0,0 +1,18 @@ +#ifndef IZ_CONFIG_WINDOW_H +#define IZ_CONFIG_WINDOW_H + +/** + * Structure that defines the application's window configuration. + */ +typedef struct { + /** + * Width of the application window + */ + unsigned int width; + /** + * Height of the application window. + */ + unsigned int height; +} IZ_ConfigWindow; + +#endif diff --git a/src/packages/game/core/IZ_constants.h b/src/packages/game/core/IZ_constants.h new file mode 100644 index 0000000..c845cd9 --- /dev/null +++ b/src/packages/game/core/IZ_constants.h @@ -0,0 +1,11 @@ +#ifndef IZ_CONSTANTS_H +#define IZ_CONSTANTS_H + +/** + * How many players can play this application? + * + * Default value: 1u + */ +#define IZ_MAX_PLAYERS ((unsigned short) 1u) + +#endif diff --git a/src/packages/game/core/IZ_movable.h b/src/packages/game/core/IZ_movable.h index 5c384b4..f75a559 100644 --- a/src/packages/game/core/IZ_movable.h +++ b/src/packages/game/core/IZ_movable.h @@ -7,4 +7,4 @@ typedef struct { IZ_Vector velocity; } IZ_Movable; -#endif //IZ_MOVABLE_H +#endif diff --git a/src/packages/game/core/IZ_placeable.h b/src/packages/game/core/IZ_placeable.h index 6b06be6..e3afe91 100644 --- a/src/packages/game/core/IZ_placeable.h +++ b/src/packages/game/core/IZ_placeable.h @@ -7,4 +7,4 @@ typedef struct { IZ_Vector location; } IZ_Placeable; -#endif //IZ_PLACEABLE_H +#endif diff --git a/src/packages/game/core/IZ_spatial.h b/src/packages/game/core/IZ_spatial.h index 3b0adcf..7fb29c8 100644 --- a/src/packages/game/core/IZ_spatial.h +++ b/src/packages/game/core/IZ_spatial.h @@ -8,4 +8,4 @@ typedef struct { IZ_Vector size; } IZ_Spatial; -#endif //IZ_SPATIAL_H +#endif diff --git a/src/packages/game/input/IZ_action.c b/src/packages/game/input/IZ_action.c new file mode 100644 index 0000000..b6e7f74 --- /dev/null +++ b/src/packages/game/input/IZ_action.c @@ -0,0 +1 @@ +#include "IZ_action.h" diff --git a/src/packages/game/input/IZ_action.h b/src/packages/game/input/IZ_action.h new file mode 100644 index 0000000..9c3a95b --- /dev/null +++ b/src/packages/game/input/IZ_action.h @@ -0,0 +1,102 @@ +#ifndef IZ_ACTION_H +#define IZ_ACTION_H + +/** + * Enumeration for the actions available in the application. + */ +typedef enum { + /** + * Index for the right (eastward) directional action. + */ + IZ_ACTION_INDEX_RIGHT = 0x0u, + /** + * Index for the down (southward) directional action. + */ + IZ_ACTION_INDEX_DOWN = 0x1u, + /** + * Index for the left (westward) directional action. + */ + IZ_ACTION_INDEX_LEFT = 0x2u, + /** + * Index for the up (northward) directional action. + */ + IZ_ACTION_INDEX_UP = 0x3u, + /** + * Index for the affirmative action. + */ + IZ_ACTION_INDEX_YES = 0x4u, + /** + * Index for the negative action. + */ + IZ_ACTION_INDEX_NO = 0x5u, + /** + * Index for the first implementation-defined action. + */ + IZ_ACTION_INDEX_ACTION0 = 0x6u, + /** + * Index for the second implementation-defined action. + */ + IZ_ACTION_INDEX_ACTION1 = 0x7u, + /** + * Index for the third implementation-defined action. + */ + IZ_ACTION_INDEX_ACTION2 = 0x8u, + /** + * Index for the fourth implementation-defined action. + */ + IZ_ACTION_INDEX_ACTION3 = 0x9u, + /** + * Index for the fifth implementation-defined action. + */ + IZ_ACTION_INDEX_ACTION4 = 0xAu, + /** + * Index for the sixth implementation-defined action. + */ + IZ_ACTION_INDEX_ACTION5 = 0xBu, + /** + * Index for the seventh implementation-defined action. + */ + IZ_ACTION_INDEX_ACTION6 = 0xCu, + /** + * Index for the eighth implementation-defined action. + */ + IZ_ACTION_INDEX_ACTION7 = 0xDu, + /** + * Index for the ninth implementation-defined action. + */ + IZ_ACTION_INDEX_ACTION8 = 0xEu, + /** + * Index for the tenth implementation-defined action. + */ + IZ_ACTION_INDEX_ACTION9 = 0xFu, +} IZ_ActionIndex; + +typedef short IZ_ActionFlag; + +inline IZ_ActionFlag IZ_ActionFlagValue(IZ_ActionIndex index) { + return (IZ_ActionFlag) (0x1 << index); +} + +/** + * Names of the actions. + */ +static const char* IZ_ACTION_NAMES[] = { + "Right", + "Down", + "Left", + "Up", + "Yes", + "No", + "Action0", + "Action1", + "Action2", + "Action3", + "Action4", + "Action5", + "Action6", + "Action7", + "Action8", + "Action9", +}; + +#endif diff --git a/src/packages/game/main.c b/src/packages/game/main.c index 23dbcac..94940a7 100644 --- a/src/packages/game/main.c +++ b/src/packages/game/main.c @@ -3,10 +3,13 @@ #include const char* APP_NAME = "SDL2"; -const int SCREEN_WIDTH = 640; -const int SCREEN_HEIGHT = 480; +const unsigned int SCREEN_WIDTH = 640; +const unsigned int SCREEN_HEIGHT = 480; +const unsigned int SCREEN_FPS = 30; -int main(int argc, char* args[]) { +#define SDL_GetTicks64 SDL_GetTicks + +int main() { SDL_Window* window = NULL; SDL_Surface* screenSurface = NULL; @@ -19,8 +22,8 @@ int main(int argc, char* args[]) { APP_NAME, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, - SCREEN_WIDTH, - SCREEN_HEIGHT, + (int) SCREEN_WIDTH, + (int) SCREEN_HEIGHT, SDL_WINDOW_SHOWN ); @@ -32,15 +35,38 @@ int main(int argc, char* args[]) { bool quit = false; SDL_Event e; screenSurface = SDL_GetWindowSurface(window); + + uint64_t start = SDL_GetTicks64(); + unsigned int seconds_elapsed = 0; + uint64_t last_update = start; + unsigned int frames = 0; while (!quit) { while (SDL_PollEvent(&e) != 0) { if (e.type == SDL_QUIT) { quit = true; } - SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 0xFF, 0xFF, 0xFF)); - SDL_UpdateWindowSurface(window); } + + unsigned int current_ticks = SDL_GetTicks64(); + unsigned int delta = current_ticks - last_update; + unsigned int new_seconds_elapsed = current_ticks / 1000; + + if (new_seconds_elapsed > seconds_elapsed) { + printf("%u\n", frames); + frames = 0; + seconds_elapsed = new_seconds_elapsed; + } + + if ((float) delta < 1000.f / (float) (SCREEN_FPS + 1)) { + continue; + } + + last_update = current_ticks; + frames += 1; + SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 0xFF, 0xFF, 0xFF)); + SDL_UpdateWindowSurface(window); } + SDL_DestroyWindow(window); SDL_Quit(); diff --git a/src/packages/game/math/IZ_math.h b/src/packages/game/math/IZ_math.h index e3c60d2..73aae82 100644 --- a/src/packages/game/math/IZ_math.h +++ b/src/packages/game/math/IZ_math.h @@ -7,4 +7,4 @@ typedef float IZ_Real; inline IZ_Real IZ_Sqrt(IZ_Real x) { return (IZ_Real) sqrtf(x); } -#endif //IZ_MATH_H +#endif diff --git a/src/packages/game/math/IZ_vector.h b/src/packages/game/math/IZ_vector.h index 23cfc5e..7c17008 100644 --- a/src/packages/game/math/IZ_vector.h +++ b/src/packages/game/math/IZ_vector.h @@ -49,4 +49,4 @@ inline IZ_Real IZ_VectorDistance(IZ_Vector a, IZ_Vector b) { return IZ_Sqrt(x * x + y * y); } -#endif //IZ_VECTOR_H +#endif