Sa marche putain!
This commit is contained in:
+18
-2
@@ -1,16 +1,32 @@
|
||||
#include "renderer.h"
|
||||
#include "text.h"
|
||||
#include <SDL2/SDL_image.h>
|
||||
#include <SDL2/SDL_surface.h>
|
||||
|
||||
int main(void) {
|
||||
renderer_main();
|
||||
RenderObject_t text;
|
||||
SDL_Color white = {128, 128, 128, 128};
|
||||
SDL_Color white = {255, 255, 255, 255};
|
||||
TTF_Init();
|
||||
if(text_renderText(&text, 0.f, 0.f, 1, white, "/home/gryf/.local/share/fonts/ARIAL.TTF", 25, "Hello World") < 0){
|
||||
if(text_renderText(&text, 0.f, 0.f, 1000, 1, white, "/usr/share/fonts/TTF/arial.ttf", 25, "Hello World") < 0){
|
||||
printf("error");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
SDL_Surface* img = IMG_Load("pp.png");
|
||||
|
||||
RenderObject_t duck = {
|
||||
0.2,
|
||||
0.2,
|
||||
0,
|
||||
0.50,
|
||||
0.50,
|
||||
0,
|
||||
img
|
||||
};
|
||||
|
||||
renderer_renderObject(text);
|
||||
renderer_renderObject(duck);
|
||||
while(1);
|
||||
|
||||
text_freeFontCache();
|
||||
|
||||
+45
-28
@@ -6,6 +6,7 @@
|
||||
#include <SDL2/SDL_render.h>
|
||||
#include <SDL2/SDL_timer.h>
|
||||
#include <bits/pthreadtypes.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
@@ -24,6 +25,7 @@ int objectCount = 0;
|
||||
int objectStorageSize = 0;
|
||||
MonitorSize_t monitorSize;
|
||||
pthread_t thread;
|
||||
pthread_mutex_t mutex;
|
||||
SDL_Window* win;
|
||||
|
||||
static int compartSort(const RenderItem_t * a, const RenderItem_t * b) {
|
||||
@@ -32,31 +34,6 @@ static int compartSort(const RenderItem_t * a, const RenderItem_t * b) {
|
||||
|
||||
//int (*compar)(const void [.size], const void [.size], void *)
|
||||
static void * render_thread(void *) {
|
||||
SDL_GetRendererOutputSize(renderer, &monitorSize.width, &monitorSize.height);
|
||||
|
||||
while(true) {
|
||||
for(int i = 0; i < objectCount; i++) {
|
||||
RenderItem_t * object = &objects[i];
|
||||
if(object->cache == NULL) {
|
||||
SDL_Texture * texture = SDL_CreateTextureFromSurface(renderer, object->object.surface);
|
||||
if(texture == NULL) {
|
||||
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_Delay(1000 / 400); //400fps or u a pussy
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void renderer_main() {
|
||||
// returns zero on success else non-zero
|
||||
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
|
||||
printf("error initializing SDL: %s\n", SDL_GetError());
|
||||
@@ -64,17 +41,57 @@ void renderer_main() {
|
||||
win = SDL_CreateWindow("GAME", // creates a window
|
||||
SDL_WINDOWPOS_CENTERED,
|
||||
SDL_WINDOWPOS_CENTERED,
|
||||
1000, 1000, 0);
|
||||
500, 500, 0);
|
||||
|
||||
Uint32 render_flags = SDL_RENDERER_ACCELERATED;
|
||||
|
||||
// creates a renderer to render our images
|
||||
renderer = SDL_CreateRenderer(win, -1, render_flags);
|
||||
|
||||
if(renderer == NULL) {
|
||||
fprintf(stderr, "Could not create renderer\n");
|
||||
}
|
||||
|
||||
SDL_GetRendererOutputSize(renderer, &monitorSize.width, &monitorSize.height);
|
||||
objectStorageSize = 1000;
|
||||
objects = malloc(objectStorageSize);
|
||||
|
||||
pthread_mutex_unlock(&mutex);
|
||||
|
||||
while(true) {
|
||||
//SDL_RenderClear(renderer);
|
||||
for(int i = 0; i < objectCount; i++) {
|
||||
RenderItem_t * object = &objects[i];
|
||||
if(object->cache == NULL) {
|
||||
SDL_Texture * texture = SDL_CreateTextureFromSurface(renderer, object->object.surface);
|
||||
if(texture == NULL) {
|
||||
fprintf(stderr, "Error while creating texture %s is_null: %d\n", SDL_GetError(), object->object.surface == NULL);
|
||||
exit(1);
|
||||
}
|
||||
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
|
||||
object->cache = texture;
|
||||
printf("Built cache\n");
|
||||
}
|
||||
|
||||
int error = SDL_RenderCopyEx(renderer, object->cache, NULL, &object->rect, 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());
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
SDL_RenderPresent(renderer);
|
||||
SDL_Delay(1000 / 400); //400fps or u a pussy
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void renderer_main() {
|
||||
|
||||
pthread_mutex_init(&mutex, NULL);
|
||||
pthread_mutex_lock(&mutex);
|
||||
pthread_create(&thread, NULL, render_thread, NULL);
|
||||
pthread_mutex_lock(&mutex);
|
||||
pthread_mutex_destroy(&mutex);
|
||||
}
|
||||
|
||||
RenderId_t renderer_renderObject(RenderObject_t object) {
|
||||
@@ -83,8 +100,8 @@ RenderId_t renderer_renderObject(RenderObject_t object) {
|
||||
SDL_Rect rect = {
|
||||
round(object.x * monitorSize.width),
|
||||
round(object.y * monitorSize.height),
|
||||
round(object.height * monitorSize.height),
|
||||
round(object.width * monitorSize.width)
|
||||
round(object.width * monitorSize.width),
|
||||
round(object.height * monitorSize.height)
|
||||
};
|
||||
|
||||
RenderItem_t item = {
|
||||
|
||||
+8
-3
@@ -1,5 +1,6 @@
|
||||
#include <SDL2/SDL_error.h>
|
||||
#include <unistd.h>
|
||||
#include "renderer.h"
|
||||
#include "text.h"
|
||||
#include <SDL2/SDL_log.h>
|
||||
|
||||
@@ -22,7 +23,7 @@ void text_freeFontCache(void){
|
||||
}
|
||||
}
|
||||
|
||||
int text_renderText(RenderObject_t* renderObject, float x, float y, int z, SDL_Color color, const char* fontFilename, int fontSize, char* format, ...){
|
||||
int text_renderText(RenderObject_t* renderObject, float x, float y, int z, float scale, 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;
|
||||
@@ -62,12 +63,16 @@ int text_renderText(RenderObject_t* renderObject, float x, float y, int z, SDL_C
|
||||
|
||||
SDL_Surface* surface = TTF_RenderText_Blended(font, text, color);
|
||||
|
||||
int h, w;
|
||||
|
||||
TTF_SizeText(font, text, &w, &h);
|
||||
MonitorSize_t monitor = renderer_getMonitorSize();
|
||||
|
||||
renderObject->x = x;
|
||||
renderObject->y = y;
|
||||
renderObject->z = z;
|
||||
renderObject->width = strlen(text) * fontSize;
|
||||
renderObject->height = fontSize;
|
||||
renderObject->width = w * scale / 500;
|
||||
renderObject->height = h * scale / 500;
|
||||
renderObject->angle_degre = 0;
|
||||
renderObject->surface = surface;
|
||||
|
||||
|
||||
+1
-2
@@ -12,8 +12,7 @@ typedef struct {
|
||||
} Position;
|
||||
|
||||
|
||||
int text_renderText(RenderObject_t* renderObject, float x, float y, int z, SDL_Color color, const char* fontFilename, int fontSize, char* format, ...);
|
||||
|
||||
int text_renderText(RenderObject_t* renderObject, float x, float y, int z, float scale, SDL_Color color, const char* fontFilename, int fontSize, char* format, ...);
|
||||
void text_freeFontCache(void);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user