From 2cc91880534b37ae6814ff92dbf8d102f51a32a6 Mon Sep 17 00:00:00 2001 From: Raimon Zamora Date: Fri, 3 Sep 2021 12:06:39 +0200 Subject: [PATCH] draw functions --- draw.cpp | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ draw.h | 16 ++++++++++ main.cpp | 29 +++++++++++++++--- 3 files changed, 132 insertions(+), 4 deletions(-) create mode 100644 draw.cpp create mode 100644 draw.h diff --git a/draw.cpp b/draw.cpp new file mode 100644 index 0000000..f051e2a --- /dev/null +++ b/draw.cpp @@ -0,0 +1,91 @@ +#include "draw.h" +#include + +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); + sdlTexture = SDL_CreateTextureFromSurface(sdlRenderer, SDL_LoadBMP("font.bmp")); +} + +void draw_quit() { + SDL_DestroyTexture(sdlTexture); + SDL_DestroyRenderer(sdlRenderer); + SDL_DestroyWindow(sdlWindow); +} + +void draw_clear() { + SDL_SetRenderDrawColor(sdlRenderer, 0, 0, 0, 255); + SDL_RenderClear(sdlRenderer); +} + +void draw_present() { + SDL_RenderPresent(sdlRenderer); +} + +void draw_fill(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_Rect rect {x1, y1, x2, y2}; + SDL_RenderFillRect(sdlRenderer, &rect); +} + +void draw_rect(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_Rect rect {x1, y1, x2, y2}; + SDL_RenderDrawRect(sdlRenderer, &rect); +} + +void draw_inset(const int x1, const int y1, const int x2, const int y2, const uint8_t color1, const uint8_t color2) { + SDL_SetRenderDrawColor(sdlRenderer, pal[color1][0], pal[color1][1], pal[color1][2], 255); + +} + +void draw_outset(const int x1, const int y1, const int x2, const int y2, const uint8_t color1, const uint8_t color2) { + SDL_SetRenderDrawColor(sdlRenderer, pal[color1][0], pal[color1][1], pal[color1][2], 255); + +} + +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_Rect rect {x1, y1, x2, y2}; + SDL_RenderFillRect(sdlRenderer, &rect); + +} + +void draw_dotted(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); + +} + +void draw_char(const char chr, const int x, const int y, const uint8_t color) { + SDL_SetRenderDrawColor(sdlRenderer, pal[color][0], pal[color][1], pal[color][2], 255); + +} + +void draw_string(const char* str, const int x, const int y, const uint8_t color) { + SDL_SetRenderDrawColor(sdlRenderer, pal[color][0], pal[color][1], pal[color][2], 255); + +} diff --git a/draw.h b/draw.h new file mode 100644 index 0000000..905e77e --- /dev/null +++ b/draw.h @@ -0,0 +1,16 @@ +#pragma once + +#include + +void draw_init(); +void draw_quit(); +void draw_clear(); +void draw_present(); +void draw_fill(const int x1, const int y1, const int x2, const int y2, const uint8_t color); +void draw_rect(const int x1, const int y1, const int x2, const int y2, const uint8_t color); +void draw_inset(const int x1, const int y1, const int x2, const int y2, const uint8_t color1, const uint8_t color2); +void draw_outset(const int x1, const int y1, const int x2, const int y2, const uint8_t color1, const uint8_t color2); +void draw_line(const int x1, const int y1, const int x2, const int y2, const uint8_t color); +void draw_dotted(const int x1, const int y1, const int x2, const int y2, const uint8_t color); +void draw_char(const char chr, const int x, const int y, const uint8_t color); +void draw_string(const char* str, const int x, const int y, const uint8_t color); diff --git a/main.cpp b/main.cpp index 095ac3e..2217df5 100755 --- a/main.cpp +++ b/main.cpp @@ -6,8 +6,26 @@ #define SOUND_NUM_SAMPLES SOUND_DURATION*SAMPLES_PER_SECOND #define SOUND_SIZE_IN_BYTES SOUND_NUM_SAMPLES*AUDIO_FORMAT_SIZE +SDL_Window *sdlWindow = nullptr; +SDL_Renderer *sdlRenderer = nullptr; +SDL_Texture *sdlTexture = nullptr; + SDL_AudioSpec audioSpec{SAMPLES_PER_SECOND, AUDIO_FORMAT, 1, 0, 512, 0, 0, NULL, NULL}; +void draw_char(const uint8_t character, const int x, const int y) { + SDL_Rect src {((character-32)%16)*4, ((character-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 int l = strlen(str); + for (int i = 0; i < l; ++i) { + draw_char(str[i], x, y); + x += 5; + } +} + int main(int argc, char* argv[]) { SDL_Init(SDL_INIT_EVERYTHING); SDL_AudioDeviceID sdlAudioDevice = SDL_OpenAudioDevice(NULL, 0, &audioSpec, NULL, 0); @@ -57,17 +75,20 @@ int main(int argc, char* argv[]) { beeps[i] += beeps2[i]; } - SDL_Window *sdlWindow = SDL_CreateWindow("Mus", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1102, 700, SDL_WINDOW_SHOWN); - SDL_Renderer *sdlRenderer = SDL_CreateRenderer(sdlWindow, -1, 0); + sdlWindow = SDL_CreateWindow("Mus", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN); + sdlRenderer = SDL_CreateRenderer(sdlWindow, -1, 0); + SDL_RenderSetLogicalSize(sdlRenderer, 320, 240); + sdlTexture = SDL_CreateTextureFromSurface(sdlRenderer, SDL_LoadBMP("font.bmp")); SDL_SetRenderDrawColor(sdlRenderer, 255, 255, 255, 255); - int last_x = 0, last_y = 120; + /*int last_x = 0, last_y = 120; for (int x=0; x<1102; ++x) { int y = 700 - (350+300*(float(beeps[x*10])/float(AUDIO_FORMAT_MAX_VALUE))); SDL_RenderDrawLine(sdlRenderer, x, y, last_x, last_y); last_x = x; last_y = y; - } + }*/ + draw_string("HOLA", 10, 10); SDL_RenderPresent(sdlRenderer); SDL_QueueAudio(sdlAudioDevice, &beeps[0], SOUND_SIZE_IN_BYTES*8);