107 lines
3.9 KiB
C++
107 lines
3.9 KiB
C++
#include "draw.h"
|
|
#include <SDL2/SDL.h>
|
|
|
|
static SDL_Window *sdlWindow = nullptr;
|
|
static SDL_Renderer *sdlRenderer = nullptr;
|
|
static SDL_Texture *sdlTexture = nullptr;
|
|
|
|
static uint8_t pal[16][3] = {
|
|
{0, 0, 0},
|
|
{29, 43, 83},
|
|
{126, 37, 83},
|
|
{0, 135, 81},
|
|
{171, 82, 54},
|
|
{95, 87, 79},
|
|
{194, 195, 199},
|
|
{255, 241, 232},
|
|
{255, 0, 77},
|
|
{255, 163, 0},
|
|
{255, 236, 39},
|
|
{0, 228, 54},
|
|
{41, 173, 255},
|
|
{131, 118, 156},
|
|
{255, 119, 168},
|
|
{255, 204, 170}
|
|
};
|
|
|
|
void draw_init() {
|
|
sdlWindow = SDL_CreateWindow("Chirping", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
|
|
sdlRenderer = SDL_CreateRenderer(sdlWindow, -1, 0);
|
|
SDL_RenderSetLogicalSize(sdlRenderer, 320, 240);
|
|
SDL_Surface *sdlSurface = SDL_LoadBMP("font.bmp");
|
|
//sdlSurface->pixels[0] = 1;
|
|
sdlSurface->format->palette->colors[0].a = 0;
|
|
sdlTexture = SDL_CreateTextureFromSurface(sdlRenderer, sdlSurface);
|
|
SDL_SetTextureBlendMode(sdlTexture, SDL_BLENDMODE_BLEND);
|
|
}
|
|
|
|
void draw_quit() {
|
|
SDL_DestroyTexture(sdlTexture);
|
|
SDL_DestroyRenderer(sdlRenderer);
|
|
SDL_DestroyWindow(sdlWindow);
|
|
}
|
|
|
|
void draw_clear(const uint8_t color) {
|
|
SDL_SetRenderDrawColor(sdlRenderer, pal[color][0], pal[color][1], pal[color][2], 255);
|
|
SDL_RenderClear(sdlRenderer);
|
|
}
|
|
|
|
void draw_present() {
|
|
SDL_RenderPresent(sdlRenderer);
|
|
}
|
|
|
|
void draw_fill(const int x, const int y, const int w, const int h, const uint8_t color) {
|
|
SDL_SetRenderDrawColor(sdlRenderer, pal[color][0], pal[color][1], pal[color][2], 255);
|
|
SDL_Rect rect {x, y, w, h};
|
|
SDL_RenderFillRect(sdlRenderer, &rect);
|
|
}
|
|
|
|
void draw_rect(const int x, const int y, const int w, const int h, const uint8_t color) {
|
|
SDL_SetRenderDrawColor(sdlRenderer, pal[color][0], pal[color][1], pal[color][2], 255);
|
|
SDL_Rect rect {x, y, w, h};
|
|
SDL_RenderDrawRect(sdlRenderer, &rect);
|
|
}
|
|
|
|
void draw_inset(const int x, const int y, const int w, const int h, const uint8_t color1, const uint8_t color2) {
|
|
SDL_SetRenderDrawColor(sdlRenderer, pal[color1][0], pal[color1][1], pal[color1][2], 255);
|
|
SDL_RenderDrawLine(sdlRenderer, x+1, y+h-1, x+w-1, y+h-1);
|
|
SDL_RenderDrawLine(sdlRenderer, x+w-1, y+1, x+w-1, y+h-1);
|
|
SDL_SetRenderDrawColor(sdlRenderer, pal[color2][0], pal[color2][1], pal[color2][2], 255);
|
|
SDL_RenderDrawLine(sdlRenderer, x, y, x, y+h-2);
|
|
SDL_RenderDrawLine(sdlRenderer, x, y, x+w-2, y);
|
|
}
|
|
|
|
void draw_outset(const int x, const int y, const int w, const int h, const uint8_t color1, const uint8_t color2) {
|
|
SDL_SetRenderDrawColor(sdlRenderer, pal[color1][0], pal[color1][1], pal[color1][2], 255);
|
|
SDL_RenderDrawLine(sdlRenderer, x, y, x, y+h-2);
|
|
SDL_RenderDrawLine(sdlRenderer, x, y, x+w-2, y);
|
|
SDL_SetRenderDrawColor(sdlRenderer, pal[color2][0], pal[color2][1], pal[color2][2], 255);
|
|
SDL_RenderDrawLine(sdlRenderer, x+1, y+h-1, x+w-1, y+h-1);
|
|
SDL_RenderDrawLine(sdlRenderer, x+w-1, y+1, x+w-1, y+h-1);
|
|
}
|
|
|
|
void draw_line(const int x1, const int y1, const int x2, const int y2, const uint8_t color) {
|
|
SDL_SetRenderDrawColor(sdlRenderer, pal[color][0], pal[color][1], pal[color][2], 255);
|
|
SDL_RenderDrawLine(sdlRenderer, x1, y1, x2, y2);
|
|
}
|
|
|
|
void draw_dotted(const int x1, const int y, const int x2, const uint8_t color) {
|
|
SDL_SetRenderDrawColor(sdlRenderer, pal[color][0], pal[color][1], pal[color][2], 255);
|
|
for (int x=x1; x<=x2;x+=2) SDL_RenderDrawPoint(sdlRenderer, x, y);
|
|
}
|
|
|
|
void draw_char(const char chr, const int x, const int y, const uint8_t color) {
|
|
SDL_SetTextureColorMod(sdlTexture, pal[color][0], pal[color][1], pal[color][2]);
|
|
SDL_Rect src {((chr-32)%16)*4, ((chr-32)/16)*5, 4, 5};
|
|
SDL_Rect dst {x, y, 4, 5};
|
|
SDL_RenderCopy(sdlRenderer, sdlTexture, &src, &dst);
|
|
}
|
|
|
|
void draw_string(const char* str, int x, const int y, const uint8_t color) {
|
|
const int l = strlen(str);
|
|
for (int i = 0; i < l; ++i) {
|
|
draw_char(str[i], x, y, color);
|
|
x += 5;
|
|
}
|
|
}
|