Added ability to draw a non-filled rectangle

This commit is contained in:
abdelrahman committed 2022-10-09 04:50:28 +01:00
1 parent c1659dcf88
commit a87eba8db7
7 files changed
+45 -12

No files matched your search

+1
View File
@@ -48,6 +48,7 @@ target_link_libraries(${PROJECT_NAME} PUBLIC
install(FILES install(FILES
${CMAKE_CURRENT_BINARY_DIR}/engine_build_config.h ${CMAKE_CURRENT_BINARY_DIR}/engine_build_config.h
include/aliases.h
include/constants.h include/constants.h
include/GameEngine.h include/GameEngine.h
include/Window.h include/Window.h
+2 -1
View File
@@ -61,13 +61,14 @@ WindowSize get_window_real_size();
*/ */
WindowDrawingArea get_window_drawing_area(); WindowDrawingArea get_window_drawing_area();
void draw_rectangle(RenderRect rect, SDL_Color color);
void fill_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 * Ensures that a rendered rectangle is clipped to the drawing area when it is
* smaller than the actual size of the window. * 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); RenderRect get_texture_asset_rect(AssetID id);
void render_texture_asset(AssetID id, RenderRect *src, RenderRect *dest); void render_texture_asset(AssetID id, RenderRect *src, RenderRect *dest);
+2
View File
@@ -37,6 +37,8 @@ Renderer *create_renderer(SDL_Window *window);
void get_render_output_size(Renderer *renderer, int32_t *w, int32_t *h); void get_render_output_size(Renderer *renderer, int32_t *w, int32_t *h);
void render_clear(const Renderer *renderer, SDL_Color color); 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, void fill_rect(const Renderer *renderer, SDL_Color color, float x, float y,
float w, float h); float w, float h);
void render_surface(const Renderer *renderer, SDL_Surface *surface, void render_surface(const Renderer *renderer, SDL_Surface *surface,
+14
View File
@@ -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
-2
View File
@@ -5,8 +5,6 @@
extern "C" { extern "C" {
#endif // !__cplusplus #endif // !__cplusplus
#define INTERNAL static
#define MAX_OBJ_COUNT 10000 #define MAX_OBJ_COUNT 10000
#define MAX_ASSET_COUNT 1000 #define MAX_ASSET_COUNT 1000
#define HASH_PRIME_01 1987 #define HASH_PRIME_01 1987
+17 -9
View File
@@ -16,6 +16,7 @@
#include "SDL_ttf.h" #include "SDL_ttf.h"
#include "SDL_video.h" #include "SDL_video.h"
#include "Window.h" #include "Window.h"
#include "aliases.h"
#include "constants.h" #include "constants.h"
#include "engine_build_config.h" #include "engine_build_config.h"
#include "reusable_ids.h" #include "reusable_ids.h"
@@ -213,12 +214,17 @@ WindowDrawingArea get_window_drawing_area() {
return GameEngine.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) { void fill_rectangle(RenderRect rect, SDL_Color color) {
fill_rect(GameEngine.window.renderer, color, rect.x, rect.y, rect.width, fill_rect(GameEngine.window.renderer, color, rect.x, rect.y, rect.width,
rect.height); 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(); WindowDrawingArea area = get_window_drawing_area();
// If the rectangle is completely out of bounds, set its width and height to 0 // 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); y_clip = (dest->y + dest->height) - (area.y + area.height);
} }
// Offset and clipping of source rectangle needs to be calculated based on the if (src) {
// ratio of the x_clip and y_clip variables to the width and height of the // Offset and clipping of source rectangle needs to be calculated based on
// destination rectangle respectively. This is because the source rectangle's // the ratio of the x_clip and y_clip variables to the width and height of
// width and height might be different from the destination's // the destination rectangle respectively. This is because the source
src->x += ((x_offset / dest->width) * src->width); // rectangle's width and height might be different from the destination's
src->y += ((y_offset / dest->height) * src->height); src->x += ((x_offset / dest->width) * src->width);
src->width *= 1 - (x_clip / dest->width); src->y += ((y_offset / dest->height) * src->height);
src->height *= 1 - (y_clip / dest->height); src->width *= 1 - (x_clip / dest->width);
src->height *= 1 - (y_clip / dest->height);
}
dest->x += x_offset; dest->x += x_offset;
dest->y += y_offset; dest->y += y_offset;
+9
View File
@@ -49,6 +49,15 @@ void render_clear(const Renderer *renderer, SDL_Color color) {
SDL_RenderClear(renderer->SDL_base_renderer); 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, void fill_rect(const Renderer *renderer, SDL_Color color, float x, float y,
float w, float h) { float w, float h) {
SDL_FRect dest = {x, y, w, h}; SDL_FRect dest = {x, y, w, h};