draw functions ready

This commit is contained in:
2021-09-03 14:04:19 +02:00
parent 2cc9188053
commit e55e4c64dd
3 changed files with 68 additions and 56 deletions

View File

@@ -28,7 +28,11 @@ 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"));
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() {
@@ -37,8 +41,8 @@ void draw_quit() {
SDL_DestroyWindow(sdlWindow);
}
void draw_clear() {
SDL_SetRenderDrawColor(sdlRenderer, 0, 0, 0, 255);
void draw_clear(const uint8_t color) {
SDL_SetRenderDrawColor(sdlRenderer, pal[color][0], pal[color][1], pal[color][2], 255);
SDL_RenderClear(sdlRenderer);
}
@@ -46,46 +50,57 @@ 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) {
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 {x1, y1, x2, y2};
SDL_Rect rect {x, y, w, h};
SDL_RenderFillRect(sdlRenderer, &rect);
}
void draw_rect(const int x1, const int y1, const int x2, const int y2, const uint8_t color) {
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 {x1, y1, x2, y2};
SDL_Rect rect {x, y, w, h};
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) {
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 x1, const int y1, const int x2, const int y2, const uint8_t color1, const uint8_t color2) {
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_Rect rect {x1, y1, x2, y2};
SDL_RenderFillRect(sdlRenderer, &rect);
SDL_RenderDrawLine(sdlRenderer, x1, y1, x2, y2);
}
void draw_dotted(const int x1, const int y1, const int x2, const int y2, const uint8_t color) {
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_SetRenderDrawColor(sdlRenderer, pal[color][0], pal[color][1], pal[color][2], 255);
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, 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, 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;
}
}

29
draw.h
View File

@@ -2,15 +2,32 @@
#include <cstdint>
#define color_black 0
#define color_dark_blue 1
#define color_dark_purple 2
#define color_dark_green 3
#define color_brown 4
#define color_dark_grey 5
#define color_light_grey 6
#define color_white 7
#define color_red 8
#define color_orange 9
#define color_yellow 10
#define color_green 11
#define color_blue 12
#define color_lavender 13
#define color_pink 14
#define color_light_peach 15
void draw_init();
void draw_quit();
void draw_clear();
void draw_clear(const uint8_t color);
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_fill(const int x, const int y, const int w, const int h, const uint8_t color);
void draw_rect(const int x, const int y, const int w, const int h, const uint8_t color);
void draw_inset(const int x, const int y, const int w, const int h, const uint8_t color1, const uint8_t color2);
void draw_outset(const int x, const int y, const int w, const int h, 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);
void draw_string(const char* str, int x, const int y, const uint8_t color);

View File

@@ -1,31 +1,14 @@
#include <SDL2/SDL.h>
#include "tonegen.h"
#include "audio.h"
#include "draw.h"
#define SOUND_DURATION 0.25f
#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);
@@ -75,21 +58,19 @@ int main(int argc, char* argv[]) {
beeps[i] += beeps2[i];
}
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);
draw_init();
draw_clear(color_dark_grey);
draw_fill(3, 14, 32, 97, color_black);
draw_inset(2, 13, 34, 99, color_light_peach, color_dark_blue);
/*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);
draw_string("HOLA", 11, 11, color_black);
draw_string("HOLA", 10, 10, color_white);
draw_present();
SDL_QueueAudio(sdlAudioDevice, &beeps[0], SOUND_SIZE_IN_BYTES*8);
SDL_PauseAudioDevice(sdlAudioDevice, 0);
@@ -102,8 +83,7 @@ int main(int argc, char* argv[]) {
}
}
SDL_DestroyRenderer(sdlRenderer);
SDL_DestroyWindow(sdlWindow);
draw_quit();
SDL_Quit();
return 0;
}