A few patches
This commit is contained in:
@@ -22,6 +22,7 @@ project(FLOPPE_FION)
|
|||||||
set(SRC
|
set(SRC
|
||||||
src/main.c
|
src/main.c
|
||||||
src/renderer.c
|
src/renderer.c
|
||||||
|
src/text.c
|
||||||
)
|
)
|
||||||
|
|
||||||
# add the executable
|
# add the executable
|
||||||
|
|||||||
+12
-1
@@ -1,6 +1,17 @@
|
|||||||
#include "renderer.h"
|
#include "renderer.h"
|
||||||
|
#include "text.h"
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
renderer_main();
|
renderer_main();
|
||||||
while(1);
|
RenderObject_t text;
|
||||||
|
SDL_Color white = {128, 128, 128, 128};
|
||||||
|
TTF_Init();
|
||||||
|
if(text_renderText(&text, 0.f, 0.f, 1, white, "/home/gryf/.local/share/fonts/ARIAL.TTF", 25, "Hello World") < 0){
|
||||||
|
printf("error");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
renderer_renderObject(text);
|
||||||
|
while(1);
|
||||||
|
|
||||||
|
text_freeFontCache();
|
||||||
}
|
}
|
||||||
|
|||||||
+14
-6
@@ -1,6 +1,7 @@
|
|||||||
#include "renderer.h"
|
#include "renderer.h"
|
||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
#include <SDL2/SDL_blendmode.h>
|
#include <SDL2/SDL_blendmode.h>
|
||||||
|
#include <SDL2/SDL_error.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>
|
||||||
@@ -33,18 +34,22 @@ static int compartSort(const RenderItem_t * a, const RenderItem_t * b) {
|
|||||||
static void * render_thread(void *) {
|
static void * render_thread(void *) {
|
||||||
SDL_GetRendererOutputSize(renderer, &monitorSize.width, &monitorSize.height);
|
SDL_GetRendererOutputSize(renderer, &monitorSize.width, &monitorSize.height);
|
||||||
|
|
||||||
objectStorageSize = 1000;
|
|
||||||
objects = malloc(objectStorageSize);
|
|
||||||
|
|
||||||
while(true) {
|
while(true) {
|
||||||
for(int i = 0; i < objectCount; i++) {
|
for(int i = 0; i < objectCount; i++) {
|
||||||
RenderItem_t * object = &objects[i];
|
RenderItem_t * object = &objects[i];
|
||||||
if(object->cache == NULL) {
|
if(object->cache == NULL) {
|
||||||
SDL_Texture * texture = SDL_CreateTextureFromSurface(renderer, object->object.surface);
|
SDL_Texture * texture = SDL_CreateTextureFromSurface(renderer, object->object.surface);
|
||||||
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
|
if(texture == NULL) {
|
||||||
object->cache = texture;
|
fprintf(stderr, "Error while creating texture %s is_null: %d\n", SDL_GetError(), object->object.surface == NULL);
|
||||||
|
}
|
||||||
|
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_NONE);
|
||||||
|
object->cache = texture;
|
||||||
|
printf("Built cache\n");
|
||||||
|
}
|
||||||
|
int error = SDL_RenderCopyEx(renderer, object->cache, &object->rect, NULL, object->object.angle_degre, NULL, SDL_FLIP_NONE);
|
||||||
|
if(error != 0) {
|
||||||
|
fprintf(stderr, "Render error in object %d with error %s\n", object->id, SDL_GetError());
|
||||||
}
|
}
|
||||||
SDL_RenderCopyEx(renderer, object->cache, &object->rect, NULL, object->object.angle_degre, NULL, SDL_FLIP_NONE);
|
|
||||||
}
|
}
|
||||||
SDL_Delay(1000 / 400); //400fps or u a pussy
|
SDL_Delay(1000 / 400); //400fps or u a pussy
|
||||||
}
|
}
|
||||||
@@ -66,6 +71,9 @@ void renderer_main() {
|
|||||||
// creates a renderer to render our images
|
// creates a renderer to render our images
|
||||||
renderer = SDL_CreateRenderer(win, -1, render_flags);
|
renderer = SDL_CreateRenderer(win, -1, render_flags);
|
||||||
|
|
||||||
|
objectStorageSize = 1000;
|
||||||
|
objects = malloc(objectStorageSize);
|
||||||
|
|
||||||
pthread_create(&thread, NULL, render_thread, NULL);
|
pthread_create(&thread, NULL, render_thread, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+14
-4
@@ -1,3 +1,5 @@
|
|||||||
|
#include <SDL2/SDL_error.h>
|
||||||
|
#include <unistd.h>
|
||||||
#include "text.h"
|
#include "text.h"
|
||||||
#include <SDL2/SDL_log.h>
|
#include <SDL2/SDL_log.h>
|
||||||
|
|
||||||
@@ -20,7 +22,11 @@ void text_freeFontCache(void){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int text_renderText(RenderObject_t* renderObject, Position position, SDL_Color color, const char* fontFilename, int fontSize, char* format, ...){
|
int text_renderText(RenderObject_t* renderObject, float x, float y, int z, SDL_Color color, const char* fontFilename, int fontSize, char* format, ...){
|
||||||
|
if(access(fontFilename, F_OK) != 0) {
|
||||||
|
fprintf(stderr, "Font file not found: %s\n", fontFilename);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
TTF_Font* font = NULL;
|
TTF_Font* font = NULL;
|
||||||
for(unsigned i = 0; i < fontCacheSize; ++i){
|
for(unsigned i = 0; i < fontCacheSize; ++i){
|
||||||
@@ -33,6 +39,7 @@ int text_renderText(RenderObject_t* renderObject, Position position, SDL_Color c
|
|||||||
font = TTF_OpenFont(fontFilename, fontSize);
|
font = TTF_OpenFont(fontFilename, fontSize);
|
||||||
if(font == NULL) {
|
if(font == NULL) {
|
||||||
SDL_LogError(SDL_LOG_PRIORITY_ERROR, "TTF_OpenFont");
|
SDL_LogError(SDL_LOG_PRIORITY_ERROR, "TTF_OpenFont");
|
||||||
|
fprintf(stderr, "Error while opening font: %s\n", SDL_GetError());
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
FontCacheObject_t fontCacheObject;
|
FontCacheObject_t fontCacheObject;
|
||||||
@@ -56,9 +63,12 @@ int text_renderText(RenderObject_t* renderObject, Position position, SDL_Color c
|
|||||||
SDL_Surface* surface = TTF_RenderText_Blended(font, text, color);
|
SDL_Surface* surface = TTF_RenderText_Blended(font, text, color);
|
||||||
|
|
||||||
|
|
||||||
renderObject->x = position.x;
|
renderObject->x = x;
|
||||||
renderObject->y = position.y;
|
renderObject->y = y;
|
||||||
renderObject->z = position.z;
|
renderObject->z = z;
|
||||||
|
renderObject->width = strlen(text) * fontSize;
|
||||||
|
renderObject->height = fontSize;
|
||||||
|
renderObject->angle_degre = 0;
|
||||||
renderObject->surface = surface;
|
renderObject->surface = surface;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
+1
-1
@@ -12,7 +12,7 @@ typedef struct {
|
|||||||
} Position;
|
} Position;
|
||||||
|
|
||||||
|
|
||||||
int text_renderText(RenderObject_t* renderObject, Position position, SDL_Color color, const char* fontFilename, int fontSize, char* format, ...);
|
int text_renderText(RenderObject_t* renderObject, float x, float y, int z, SDL_Color color, const char* fontFilename, int fontSize, char* format, ...);
|
||||||
|
|
||||||
void text_freeFontCache(void);
|
void text_freeFontCache(void);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user