draw rectangles

This commit is contained in:
KikooDX 2022-02-28 21:47:26 +01:00
parent ea2fe72558
commit 426a48adb9
2 changed files with 63 additions and 10 deletions

View File

@ -14,6 +14,8 @@ int LZY_DrawEnd(void);
void LZY_DrawSetColor(uint8_t r, uint8_t g, uint8_t b);
void LZY_DrawSetColorNone(void);
int LZY_DrawClear(void);
int LZY_DrawRect(int x, int y, unsigned int w, unsigned int h);
int LZY_DrawFillRect(int x, int y, unsigned int w, unsigned int h);
void LZY_CycleEvents(void);
int LZY_KeyDown(unsigned int key);
int LZY_ShouldQuit(void);
@ -49,7 +51,7 @@ enum LZY_ScanCode {
#define LZY_TILE_SIZE 16
#endif
#ifndef LZY_DISPLAY_WIDTH
#define LZY_DISPLAY_WIDTH 400
#define LZY_DISPLAY_WIDTH 396
#endif
#ifndef LZY_DISPLAY_HEIGHT
#define LZY_DISPLAY_HEIGHT 224
@ -116,6 +118,20 @@ int LZY_DrawClear(void) {
return 0;
}
int LZY_DrawRect(int x, int y, unsigned int w, unsigned int h) {
if (w < 1 || h < 1)
return -1;
drect_border(x, y, x + w - 1, y + h - 1, C_NONE, 1, draw_color);
return 0;
}
int LZY_DrawFillRect(int x, int y, unsigned int w, unsigned int h) {
if (w < 1 || h < 1)
return -1;
drect(x, y, x + w - 1, y + h - 1, draw_color);
return 0;
}
void LZY_CycleEvents(void) {
clearevents();
should_quit = should_quit || keydown(KEY_EXIT);
@ -153,7 +169,7 @@ static uint64_t next_time;
int LZY_Init(const char *title, int target_fps) {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
error = SDL_GetError();
return 1;
return -1;
}
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, 0);
@ -163,7 +179,7 @@ int LZY_Init(const char *title, int target_fps) {
LZY_DISPLAY_HEIGHT, SDL_WINDOW_SHOWN);
if (window == NULL) {
error = SDL_GetError();
return 2;
return -2;
}
SDL_SetWindowMinimumSize(window, LZY_DISPLAY_WIDTH, LZY_DISPLAY_HEIGHT);
@ -171,7 +187,7 @@ int LZY_Init(const char *title, int target_fps) {
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (renderer == NULL) {
error = SDL_GetError();
return 3;
return -3;
}
fps_limiter = target_fps > 0;
@ -227,7 +243,25 @@ void LZY_DrawSetColorNone(void) {
int LZY_DrawClear(void) {
if (SDL_RenderClear(renderer) < 0) {
error = SDL_GetError();
return 1;
return -1;
}
return 0;
}
int LZY_DrawRect(int x, int y, unsigned int w, unsigned int h) {
const SDL_Rect rect = {x, y, w, h};
if (SDL_RenderDrawRect(renderer, &rect) < 0) {
error = SDL_GetError();
return -1;
}
return 0;
}
int LZY_DrawFillRect(int x, int y, unsigned int w, unsigned int h) {
const SDL_Rect rect = {x, y, w, h};
if (SDL_RenderFillRect(renderer, &rect) < 0) {
error = SDL_GetError();
return -1;
}
return 0;
}

View File

@ -2,8 +2,11 @@
#include "lzy.h"
#include <stdint.h>
static void draw_player(int x, int y);
int main(void) {
uint8_t c = 0;
int x = 0;
int y = 0;
if (LZY_Init("lzy example", 30)) {
LZY_Log(LZY_GetError());
@ -13,15 +16,24 @@ int main(void) {
LZY_Log("init was great success!");
do {
/* update */
LZY_CycleEvents();
if (LZY_KeyDown(LZYK_LEFT))
c -= 8;
x--;
if (LZY_KeyDown(LZYK_RIGHT))
c += 8;
x++;
if (LZY_KeyDown(LZYK_UP))
y--;
if (LZY_KeyDown(LZYK_DOWN))
y++;
/* draw */
LZY_DrawBegin();
LZY_DrawSetColor(c, c, c);
LZY_DrawClear();
{
LZY_DrawSetColor(0x20, 0x20, 0x00);
LZY_DrawClear();
draw_player(x, y);
}
LZY_DrawEnd();
} while (!LZY_ShouldQuit());
@ -29,3 +41,10 @@ int main(void) {
LZY_Quit();
return 0;
}
static void draw_player(int x, int y) {
LZY_DrawSetColor(0x00, 0xff, 0xff);
LZY_DrawFillRect(x, y, 8, 8);
LZY_DrawSetColor(0xff, 0x00, 0x00);
LZY_DrawRect(x + 1, y + 1, 6, 6);
}