Sa marche putain!
This commit is contained in:
+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 = {
|
||||
|
||||
Reference in New Issue
Block a user