Added proof of concept code to render some text to the screen. Split up the two 'screens' that'll be using to keep the code clean.
This commit is contained in:
@@ -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
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
#ifndef _CPU_DISPLAY_H
|
||||||
|
#define _CPU_DISPLAY_H
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
#include <SDL2/SDL_rect.h>
|
||||||
|
#include <SDL2/SDL_render.h>
|
||||||
|
#include <SDL2/SDL_surface.h>
|
||||||
|
#include <SDL2/SDL_ttf.h>
|
||||||
|
#include <SDL2/SDL_pixels.h>
|
||||||
|
|
||||||
|
#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
|
||||||
+6
-1
@@ -1,13 +1,18 @@
|
|||||||
#ifndef _VSCREEN_H
|
#ifndef _VSCREEN_H
|
||||||
#define _VSCREEN_H
|
#define _VSCREEN_H
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
|
#include <SDL2/SDL_image.h>
|
||||||
|
#include <SDL2/SDL_rect.h>
|
||||||
|
#include <SDL2/SDL_render.h>
|
||||||
|
|
||||||
#define VSCREEN_WIDTH 640
|
#define VSCREEN_WIDTH 640
|
||||||
#define VSCREEN_HEIGHT 480
|
#define VSCREEN_HEIGHT 480
|
||||||
#define VSCREEN_COLUMN_COUNT 20
|
#define VSCREEN_COLUMN_COUNT 40
|
||||||
#define VSCREEN_ROW_COUNT 20
|
#define VSCREEN_ROW_COUNT 20
|
||||||
|
|
||||||
void renderClientScreen(SDL_Renderer *);
|
void renderClientScreen(SDL_Renderer *);
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
+33
-8
@@ -1,14 +1,16 @@
|
|||||||
#include "SDL2/SDL.h"
|
#include "SDL2/SDL.h"
|
||||||
#include <SDL2/SDL_events.h>
|
#include <SDL2/SDL_events.h>
|
||||||
#include <SDL2/SDL_hints.h>
|
#include <SDL2/SDL_hints.h>
|
||||||
|
#include <SDL2/SDL_keycode.h>
|
||||||
#include <SDL2/SDL_rect.h>
|
#include <SDL2/SDL_rect.h>
|
||||||
#include <SDL2/SDL_render.h>
|
#include <SDL2/SDL_render.h>
|
||||||
#include <SDL2/SDL_timer.h>
|
#include <SDL2/SDL_timer.h>
|
||||||
#include <SDL2/SDL_video.h>
|
#include <SDL2/SDL_video.h>
|
||||||
#include <SDL2/SDL_image.h>
|
#include <SDL2/SDL_image.h>
|
||||||
|
#include <SDL2/SDL_ttf.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
|
||||||
#include "../include/vscreen.h"
|
#include "../include/vscreen.h"
|
||||||
|
#include "../include/cpu_display.h"
|
||||||
|
|
||||||
const int SCREEN_WIDTH = 1280;
|
const int SCREEN_WIDTH = 1280;
|
||||||
const int SCREEN_HEIGHT = 720;
|
const int SCREEN_HEIGHT = 720;
|
||||||
@@ -16,14 +18,12 @@ const int SCREEN_HEIGHT = 720;
|
|||||||
typedef struct {
|
typedef struct {
|
||||||
SDL_Window *window;
|
SDL_Window *window;
|
||||||
SDL_Renderer *renderer;
|
SDL_Renderer *renderer;
|
||||||
int up;
|
TTF_Font* font;
|
||||||
int down;
|
|
||||||
int left;
|
|
||||||
int right;
|
|
||||||
} App;
|
} App;
|
||||||
|
|
||||||
App app;
|
App app;
|
||||||
|
|
||||||
|
|
||||||
void handleInput(void);
|
void handleInput(void);
|
||||||
void cleanup(void);
|
void cleanup(void);
|
||||||
void prepareScene(void);
|
void prepareScene(void);
|
||||||
@@ -33,8 +33,6 @@ int getIndex(int, int, int);
|
|||||||
int main(int argc, char* args[]) {
|
int main(int argc, char* args[]) {
|
||||||
memset(&app, 0, sizeof(app));
|
memset(&app, 0, sizeof(app));
|
||||||
|
|
||||||
IMG_Init(IMG_INIT_JPG);
|
|
||||||
|
|
||||||
atexit(cleanup);
|
atexit(cleanup);
|
||||||
|
|
||||||
if (SDL_Init(SDL_INIT_VIDEO) < 0){
|
if (SDL_Init(SDL_INIT_VIDEO) < 0){
|
||||||
@@ -42,6 +40,18 @@ int main(int argc, char* args[]) {
|
|||||||
return -1;
|
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);
|
app.window = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
|
||||||
|
|
||||||
if (app.window == NULL){
|
if (app.window == NULL){
|
||||||
@@ -66,12 +76,16 @@ int main(int argc, char* args[]) {
|
|||||||
presentScene();
|
presentScene();
|
||||||
|
|
||||||
SDL_Delay(16);
|
SDL_Delay(16);
|
||||||
|
|
||||||
|
//break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void cleanup(void) {
|
void cleanup(void) {
|
||||||
SDL_DestroyWindow(app.window);
|
SDL_DestroyWindow(app.window);
|
||||||
SDL_DestroyRenderer(app.renderer);
|
SDL_DestroyRenderer(app.renderer);
|
||||||
|
TTF_CloseFont(app.font);
|
||||||
|
TTF_Quit();
|
||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,6 +97,8 @@ void handleInput(void) {
|
|||||||
case SDL_QUIT:
|
case SDL_QUIT:
|
||||||
exit(0);
|
exit(0);
|
||||||
break;
|
break;
|
||||||
|
case SDL_MOUSEBUTTONDOWN:
|
||||||
|
exit(0);
|
||||||
case SDL_KEYDOWN:
|
case SDL_KEYDOWN:
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
@@ -95,11 +111,20 @@ void prepareScene(void)
|
|||||||
SDL_SetRenderDrawColor(app.renderer, 96, 128, 255, 255);
|
SDL_SetRenderDrawColor(app.renderer, 96, 128, 255, 255);
|
||||||
SDL_RenderClear(app.renderer);
|
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);
|
SDL_RenderSetViewport(app.renderer, &rect);
|
||||||
|
|
||||||
renderClientScreen(app.renderer);
|
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)
|
void presentScene(void)
|
||||||
|
|||||||
+10
-9
@@ -1,8 +1,4 @@
|
|||||||
#include "../include/vscreen.h"
|
#include "../include/vscreen.h"
|
||||||
#include <SDL2/SDL_image.h>
|
|
||||||
#include <SDL2/SDL_render.h>
|
|
||||||
#include <SDL2/SDL_surface.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
void renderClientScreen(SDL_Renderer* renderer) {
|
void renderClientScreen(SDL_Renderer* renderer) {
|
||||||
SDL_Rect dest;
|
SDL_Rect dest;
|
||||||
@@ -14,22 +10,27 @@ void renderClientScreen(SDL_Renderer* renderer) {
|
|||||||
cell.h = dest.h / VSCREEN_ROW_COUNT;
|
cell.h = dest.h / VSCREEN_ROW_COUNT;
|
||||||
|
|
||||||
int row = 0;
|
int row = 0;
|
||||||
unsigned char backgroundColor = 0x00;
|
unsigned int bg = 0x00000000;
|
||||||
|
|
||||||
for (int i = 0; i < VSCREEN_ROW_COUNT; i++) {
|
for (int i = 0; i < VSCREEN_ROW_COUNT; i++) {
|
||||||
|
|
||||||
for(int j = 0; j <VSCREEN_COLUMN_COUNT; j++) {
|
for(int j = 0; j < VSCREEN_COLUMN_COUNT; j++) {
|
||||||
SDL_SetRenderDrawColor(renderer, backgroundColor, backgroundColor, backgroundColor, 255);
|
char red, green, blue;
|
||||||
|
red = bg >> 16 & 0xFF;
|
||||||
|
green = bg >> 8 & 0xFF;
|
||||||
|
blue = bg & 0xFF;
|
||||||
|
|
||||||
|
SDL_SetRenderDrawColor(renderer, red, green, blue, 255);
|
||||||
|
|
||||||
cell.x = j * cell.w;
|
cell.x = j * cell.w;
|
||||||
cell.y = i * cell.h;
|
cell.y = i * cell.h;
|
||||||
|
|
||||||
SDL_RenderFillRect(renderer, &cell);
|
SDL_RenderFillRect(renderer, &cell);
|
||||||
|
|
||||||
backgroundColor ^= 0xFF;
|
bg ^= 0xFFFFFF;
|
||||||
}
|
}
|
||||||
|
|
||||||
backgroundColor ^= 0xFF;
|
bg ^= 0xFFFFFF;
|
||||||
|
|
||||||
row++;
|
row++;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user