diff --git a/CMakeLists.txt b/CMakeLists.txt index 9516207..3a7a1a4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,6 +48,7 @@ target_link_libraries(${PROJECT_NAME} PUBLIC install(FILES ${CMAKE_CURRENT_BINARY_DIR}/engine_build_config.h + include/aliases.h include/constants.h include/GameEngine.h include/Window.h diff --git a/include/GameEngine.h b/include/GameEngine.h index 19da54e..5c4ac72 100644 --- a/include/GameEngine.h +++ b/include/GameEngine.h @@ -61,13 +61,14 @@ WindowSize get_window_real_size(); */ WindowDrawingArea get_window_drawing_area(); +void draw_rectangle(RenderRect rect, SDL_Color color); void fill_rectangle(RenderRect rect, SDL_Color color); /** * Ensures that a rendered rectangle is clipped to the drawing area when it is * smaller than the actual size of the window. */ -void clip_source_rect_to_drawing_area(RenderRect *src, RenderRect *dest); +void clip_render_rects_to_drawing_area(RenderRect *src, RenderRect *dest); RenderRect get_texture_asset_rect(AssetID id); void render_texture_asset(AssetID id, RenderRect *src, RenderRect *dest); diff --git a/include/Renderer.h b/include/Renderer.h index 75631eb..dc648f8 100644 --- a/include/Renderer.h +++ b/include/Renderer.h @@ -37,6 +37,8 @@ Renderer *create_renderer(SDL_Window *window); void get_render_output_size(Renderer *renderer, int32_t *w, int32_t *h); void render_clear(const Renderer *renderer, SDL_Color color); +void draw_rect(const Renderer *renderer, SDL_Color color, float x, float y, + float w, float h); void fill_rect(const Renderer *renderer, SDL_Color color, float x, float y, float w, float h); void render_surface(const Renderer *renderer, SDL_Surface *surface, diff --git a/include/aliases.h b/include/aliases.h new file mode 100644 index 0000000..50a6b8c --- /dev/null +++ b/include/aliases.h @@ -0,0 +1,14 @@ +#ifndef ALIASES_H +#define ALIASES_H + +#ifdef __cplusplus +extern "C" { +#endif // !__cplusplus + +#define INTERNAL static + +#ifdef __cplusplus +} +#endif // !__cplusplus + +#endif // !ALIASES_H diff --git a/include/constants.h b/include/constants.h index 40085b7..04c9d8e 100644 --- a/include/constants.h +++ b/include/constants.h @@ -5,8 +5,6 @@ extern "C" { #endif // !__cplusplus -#define INTERNAL static - #define MAX_OBJ_COUNT 10000 #define MAX_ASSET_COUNT 1000 #define HASH_PRIME_01 1987 diff --git a/src/GameEngine.c b/src/GameEngine.c index 48c911e..7e0fa14 100644 --- a/src/GameEngine.c +++ b/src/GameEngine.c @@ -16,6 +16,7 @@ #include "SDL_ttf.h" #include "SDL_video.h" #include "Window.h" +#include "aliases.h" #include "constants.h" #include "engine_build_config.h" #include "reusable_ids.h" @@ -213,12 +214,17 @@ WindowDrawingArea get_window_drawing_area() { return GameEngine.window.drawing_area; } +void draw_rectangle(RenderRect rect, SDL_Color color) { + draw_rect(GameEngine.window.renderer, color, rect.x, rect.y, rect.width, + rect.height); +} + void fill_rectangle(RenderRect rect, SDL_Color color) { fill_rect(GameEngine.window.renderer, color, rect.x, rect.y, rect.width, rect.height); } -void clip_source_rect_to_drawing_area(RenderRect *src, RenderRect *dest) { +void clip_render_rects_to_drawing_area(RenderRect *src, RenderRect *dest) { WindowDrawingArea area = get_window_drawing_area(); // If the rectangle is completely out of bounds, set its width and height to 0 @@ -256,14 +262,16 @@ void clip_source_rect_to_drawing_area(RenderRect *src, RenderRect *dest) { y_clip = (dest->y + dest->height) - (area.y + area.height); } - // Offset and clipping of source rectangle needs to be calculated based on the - // ratio of the x_clip and y_clip variables to the width and height of the - // destination rectangle respectively. This is because the source rectangle's - // width and height might be different from the destination's - src->x += ((x_offset / dest->width) * src->width); - src->y += ((y_offset / dest->height) * src->height); - src->width *= 1 - (x_clip / dest->width); - src->height *= 1 - (y_clip / dest->height); + if (src) { + // Offset and clipping of source rectangle needs to be calculated based on + // the ratio of the x_clip and y_clip variables to the width and height of + // the destination rectangle respectively. This is because the source + // rectangle's width and height might be different from the destination's + src->x += ((x_offset / dest->width) * src->width); + src->y += ((y_offset / dest->height) * src->height); + src->width *= 1 - (x_clip / dest->width); + src->height *= 1 - (y_clip / dest->height); + } dest->x += x_offset; dest->y += y_offset; diff --git a/src/Renderer.c b/src/Renderer.c index eda2016..1a42c42 100644 --- a/src/Renderer.c +++ b/src/Renderer.c @@ -49,6 +49,15 @@ void render_clear(const Renderer *renderer, SDL_Color color) { SDL_RenderClear(renderer->SDL_base_renderer); } +void draw_rect(const Renderer *renderer, SDL_Color color, float x, float y, + float w, float h) { + SDL_FRect dest = {x, y, w, h}; + + SDL_SetRenderDrawColor(renderer->SDL_base_renderer, color.r, color.g, color.b, + color.a); + SDL_RenderDrawRectF(renderer->SDL_base_renderer, &dest); +} + void fill_rect(const Renderer *renderer, SDL_Color color, float x, float y, float w, float h) { SDL_FRect dest = {x, y, w, h};