diff --git a/build.sh b/build.sh index a99fdfd..f4b8f6e 100755 --- a/build.sh +++ b/build.sh @@ -1 +1 @@ -gcc -o main src/main.c -lSDL2 +gcc src/main.c src/vscreen.c src/cpu_display.c -o main -lSDL2 -lSDL2_image -lSDL2_ttf \ No newline at end of file diff --git a/include/cpu_display.h b/include/cpu_display.h new file mode 100644 index 0000000..19889e0 --- /dev/null +++ b/include/cpu_display.h @@ -0,0 +1,22 @@ +#ifndef _CPU_DISPLAY_H +#define _CPU_DISPLAY_H + +#include +#include +#include +#include +#include +#include +#include + +#define CPU_STATS_DISPLAY_WIDTH 640 +#define CPU_STATS_DISPLAY_HEIGHT 480 +#define CPU_STATS_DISPLAY_COLUMN_COUNT 40 +#define CPU_STATS_DISPLAY_ROW_COUNT 30 + +static const char *cpu_display_header_text = "CPU State"; + +void renderStatsBox(SDL_Renderer *, TTF_Font *, char *); +void renderBorderAndHeader(SDL_Renderer *, TTF_Font *); + +#endif \ No newline at end of file diff --git a/include/vscreen.h b/include/vscreen.h index c13def8..7756523 100644 --- a/include/vscreen.h +++ b/include/vscreen.h @@ -1,13 +1,18 @@ #ifndef _VSCREEN_H #define _VSCREEN_H +#include #include +#include +#include +#include #define VSCREEN_WIDTH 640 #define VSCREEN_HEIGHT 480 -#define VSCREEN_COLUMN_COUNT 20 +#define VSCREEN_COLUMN_COUNT 40 #define VSCREEN_ROW_COUNT 20 void renderClientScreen(SDL_Renderer *); + #endif \ No newline at end of file diff --git a/src/cpu_display.c b/src/cpu_display.c new file mode 100644 index 0000000..d83209e --- /dev/null +++ b/src/cpu_display.c @@ -0,0 +1,38 @@ +#include "../include/cpu_display.h" + +void renderStatsBox(SDL_Renderer* renderer, TTF_Font* font, char* msg) { + SDL_Rect dest, text_rect; + SDL_Color fg = {.r = 0, .g = 255, .b = 0, .a = 255}; + SDL_Surface* text = TTF_RenderText_Solid(font, msg, fg); + TTF_SizeText(font, msg, &text_rect.w, &text_rect.h); + SDL_Texture* tex = SDL_CreateTextureFromSurface(renderer, text); + + SDL_RenderGetViewport(renderer, &dest); + text_rect.x = 0; + text_rect.y = 0; + + dest.x = 0; + dest.y = 0; + + SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); + + SDL_RenderFillRect(renderer, &dest); + + //SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255); + + SDL_RenderCopy(renderer, tex, NULL, &text_rect); + + SDL_FreeSurface(text); + SDL_DestroyTexture(tex); +} + +void renderBorderAndHeader(SDL_Renderer* renderer, TTF_Font* font) { + SDL_Rect viewPort; + + SDL_RenderGetViewport(renderer, &viewPort); + + SDL_Rect cell = {.x = 0, .y = 0}; + + cell.w = viewPort.w / CPU_STATS_DISPLAY_WIDTH; + cell.h = viewPort.h / CPU_STATS_DISPLAY_HEIGHT; +} \ No newline at end of file diff --git a/src/main.c b/src/main.c index 1c5d5ae..658b589 100644 --- a/src/main.c +++ b/src/main.c @@ -1,14 +1,16 @@ #include "SDL2/SDL.h" #include #include +#include #include #include #include #include #include +#include #include -#include #include "../include/vscreen.h" +#include "../include/cpu_display.h" const int SCREEN_WIDTH = 1280; const int SCREEN_HEIGHT = 720; @@ -16,14 +18,12 @@ const int SCREEN_HEIGHT = 720; typedef struct { SDL_Window *window; SDL_Renderer *renderer; - int up; - int down; - int left; - int right; + TTF_Font* font; } App; App app; + void handleInput(void); void cleanup(void); void prepareScene(void); @@ -33,8 +33,6 @@ int getIndex(int, int, int); int main(int argc, char* args[]) { memset(&app, 0, sizeof(app)); - IMG_Init(IMG_INIT_JPG); - atexit(cleanup); if (SDL_Init(SDL_INIT_VIDEO) < 0){ @@ -42,6 +40,18 @@ int main(int argc, char* args[]) { return -1; } + if (TTF_Init() < 0) { + printf("Failed to init TTF: %s\n", TTF_GetError()); + return -1; + } + + app.font = TTF_OpenFont("/usr/share/fonts/TTF/Hack-Regular.ttf", 14); + + if (app.font == NULL) { + printf("Failed to load font\n"); + return -1; + } + app.window = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); if (app.window == NULL){ @@ -66,12 +76,16 @@ int main(int argc, char* args[]) { presentScene(); SDL_Delay(16); + + //break; } } void cleanup(void) { SDL_DestroyWindow(app.window); SDL_DestroyRenderer(app.renderer); + TTF_CloseFont(app.font); + TTF_Quit(); SDL_Quit(); } @@ -83,6 +97,8 @@ void handleInput(void) { case SDL_QUIT: exit(0); break; + case SDL_MOUSEBUTTONDOWN: + exit(0); case SDL_KEYDOWN: default: break; @@ -95,11 +111,20 @@ void prepareScene(void) SDL_SetRenderDrawColor(app.renderer, 96, 128, 255, 255); SDL_RenderClear(app.renderer); - SDL_Rect rect = {0, 0, VSCREEN_WIDTH, VSCREEN_HEIGHT}; + SDL_Rect rect = {.x=0, .y=0, .w=VSCREEN_WIDTH, .h=VSCREEN_HEIGHT}; SDL_RenderSetViewport(app.renderer, &rect); renderClientScreen(app.renderer); + + rect.x = VSCREEN_WIDTH; + rect.w = SCREEN_WIDTH - VSCREEN_WIDTH; + rect.y = 0; + rect.h = VSCREEN_HEIGHT; + + SDL_RenderSetViewport(app.renderer, &rect); + + renderStatsBox(app.renderer, app.font, "Hello, World!"); } void presentScene(void) diff --git a/src/vscreen.c b/src/vscreen.c index ffc5a04..ed8810e 100644 --- a/src/vscreen.c +++ b/src/vscreen.c @@ -1,8 +1,4 @@ #include "../include/vscreen.h" -#include -#include -#include -#include void renderClientScreen(SDL_Renderer* renderer) { SDL_Rect dest; @@ -14,22 +10,27 @@ void renderClientScreen(SDL_Renderer* renderer) { cell.h = dest.h / VSCREEN_ROW_COUNT; int row = 0; - unsigned char backgroundColor = 0x00; + unsigned int bg = 0x00000000; for (int i = 0; i < VSCREEN_ROW_COUNT; i++) { - for(int j = 0; j > 16 & 0xFF; + green = bg >> 8 & 0xFF; + blue = bg & 0xFF; + + SDL_SetRenderDrawColor(renderer, red, green, blue, 255); cell.x = j * cell.w; cell.y = i * cell.h; SDL_RenderFillRect(renderer, &cell); - backgroundColor ^= 0xFF; + bg ^= 0xFFFFFF; } - backgroundColor ^= 0xFF; + bg ^= 0xFFFFFF; row++; }