# HG changeset patch # User David Demelier # Date 1578312412 -3600 # Node ID 03f6d572fd1711818e51952fd16a41d49cc73032 # Parent efcc908bca21132b8bdb0dafdd7bc8582520fdc8 core: implement basic drawing, closes #2438 diff -r efcc908bca21 -r 03f6d572fd17 src/main.c --- a/src/main.c Mon Jan 06 12:58:49 2020 +0100 +++ b/src/main.c Mon Jan 06 13:06:52 2020 +0100 @@ -16,17 +16,26 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#include + #include "window.h" int main(int argc, char **argv) { - (void)argc; - (void)argv; + (void)argc; + (void)argv; - struct window *w = window_init("Molko's Adventure", 640, 480); + struct window *w = window_init("Molko's Adventure", 640, 480); - SDL_Delay(5000); + window_set_color(w, 0x667788ff); + window_clear(w); + window_set_color(w, 0xffffffff); + window_draw_line(w, 50, 50, 100, 100); + window_draw_point(w, 60, 60); + window_draw_rectangle(w, true, 20, 20, 70, 10); + window_present(w); + SDL_Delay(5000); - return 0; + return 0; } diff -r efcc908bca21 -r 03f6d572fd17 src/window.c --- a/src/window.c Mon Jan 06 12:58:49 2020 +0100 +++ b/src/window.c Mon Jan 06 13:06:52 2020 +0100 @@ -49,6 +49,81 @@ return NULL; } +uint32_t +window_get_color(const struct window *w) +{ + assert(w); + + Uint8 r = 0, g = 0, b = 0, a = 0; + + SDL_GetRenderDrawColor(w->renderer, &r, &g, &b, &a); + + return r << 24 | g << 16 | b << 8 | a; +} + +void +window_set_color(struct window *w, uint32_t color) +{ + assert(w); + + const Uint8 r = color >> 24 & 0xff; + const Uint8 g = color >> 16 & 0xff; + const Uint8 b = color >> 8 & 0xff; + const Uint8 a = color & 0xff; + + SDL_SetRenderDrawColor(w->renderer, r, g, b, a); +} + +void +window_draw_line(struct window *w, int x1, int y1, int x2, int y2) +{ + assert(w); + + SDL_RenderDrawLine(w->renderer, x1, y1, x2, y2); +} + +void +window_draw_point(struct window *w, int x1, int y1) +{ + assert(w); + + SDL_RenderDrawPoint(w->renderer, x1, y1); +} + +void +window_draw_rectangle(struct window *w, bool fill, int x, int y, unsigned width, unsigned height) +{ + const SDL_Rect rect = { + .w = width, + .h = height, + .x = x, + .y = y + }; + + assert(w); + + if (fill) + SDL_RenderFillRect(w->renderer, &rect); + else + SDL_RenderDrawRect(w->renderer, &rect); +} + +void +window_clear(struct window *window) +{ + assert(window); + + SDL_RenderClear(window->renderer); +} + +void +window_present(struct window *window) +{ + assert(window); + + SDL_RenderPresent(window->renderer); +} + void window_close(struct window *w) { diff -r efcc908bca21 -r 03f6d572fd17 src/window.h --- a/src/window.h Mon Jan 06 12:58:49 2020 +0100 +++ b/src/window.h Mon Jan 06 13:06:52 2020 +0100 @@ -19,13 +19,119 @@ #ifndef MOLKO_WINDOW_H #define MOLKO_WINDOW_H +/** + * \file window.h + * \brief Basic window management. + */ + #include +#include +/** + * \brief Window opaque object. + */ struct window; +/** + * Initialize window. + * + * \pre title != NULL + * \param title the window title + * \param width the desired width + * \param height the desired height + * \return the window or NULL on error + */ struct window * -window_init(const char *name, unsigned width, unsigned height); +window_init(const char *title, unsigned width, unsigned height); + +/** + * Get the current drawing color. + * + * \pre window != NULL + * \param window the window + * \return the color in RRGGBBAA format + */ +uint32_t +window_get_color(const struct window *window); + +/** + * Set the rendering drawing color. + * + * \pre window != NULL + * \param window the window + * \param color in RRGGBBAA format + */ +void +window_set_color(struct window *window, uint32_t color); + +/** + * Draw a line. + * + * \pre window != NULL + * \param window the window + * \param x1 first X coordinate + * \param y1 first Y coordinate + * \param x2 second X coordinate + * \param y2 second Y coordinate + */ +void +window_draw_line(struct window *window, int x1, int y1, int x2, int y2); +/** + * Draw a pixel point. + * + * \pre window != NULL + * \param window the window + * \param x the X coordinate + * \param y the Y coordinate + */ +void +window_draw_point(struct window *window, int x, int y); + +/** + * Draw a rectangle + * + * \pre window != NULL + * \param window the window + * \param fill set to true to fill the rectangle + * \param x the X coordinate + * \param y the Y coordinate + * \param w the rectangle width + * \param h the rectangle height + */ +void +window_draw_rectangle(struct window *window, + bool fill, + int x, + int y, + unsigned w, + unsigned h); + +/** + * Clear the window. + * + * \pre window != NULL + * \param window the window + */ +void +window_clear(struct window *window); + +/** + * Present the window, only call this function one time in the main loop. + * + * \pre window != NULL + * \param window the window + */ +void +window_present(struct window *window); + +/** + * Close the window and destroy associated resources, do not use pointer + * afterwards. + * + * \pre window != NULL + * \param window the window + */ void window_close(struct window *window);