Merge branch 'master' of ssh.marcbarbier.fr:Marc/gamejam-polytech
This commit is contained in:
+3
-1
@@ -28,6 +28,8 @@ set(SRC
|
|||||||
# add the executable
|
# add the executable
|
||||||
add_executable(FLOPPE_FION ${SRC})
|
add_executable(FLOPPE_FION ${SRC})
|
||||||
|
|
||||||
target_link_libraries(FLOPPE_FION SDL2 SDL_image SDL2_ttf SDL2_mixer pthread m)
|
|
||||||
|
target_link_libraries(FLOPPE_FION SDL2 SDL2_image SDL2_ttf SDL2_mixer pthread m)
|
||||||
|
|
||||||
|
|
||||||
set_property(TARGET FLOPPE_FION PROPERTY C_STANDARD 23)
|
set_property(TARGET FLOPPE_FION PROPERTY C_STANDARD 23)
|
||||||
|
|||||||
+18
-2
@@ -1,6 +1,8 @@
|
|||||||
#include "renderer.h"
|
#include "renderer.h"
|
||||||
#include "text.h"
|
#include "text.h"
|
||||||
#include <SDL2/SDL_mixer.h>
|
#include <SDL2/SDL_mixer.h>
|
||||||
|
#include <SDL2/SDL_image.h>
|
||||||
|
#include <SDL2/SDL_surface.h>
|
||||||
|
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
@@ -21,13 +23,27 @@ int main(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
RenderObject_t text;
|
RenderObject_t text;
|
||||||
SDL_Color white = {128, 128, 128, 128};
|
SDL_Color white = {255, 255, 255, 255};
|
||||||
TTF_Init();
|
TTF_Init();
|
||||||
if(text_renderText(&text, 0.f, 0.f, 1, white, "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");
|
printf("error");
|
||||||
exit(1);
|
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(text);
|
||||||
|
renderer_renderObject(duck);
|
||||||
while(1);
|
while(1);
|
||||||
Mix_FreeMusic(music);
|
Mix_FreeMusic(music);
|
||||||
Mix_CloseAudio();
|
Mix_CloseAudio();
|
||||||
|
|||||||
+45
-28
@@ -6,6 +6,7 @@
|
|||||||
#include <SDL2/SDL_render.h>
|
#include <SDL2/SDL_render.h>
|
||||||
#include <SDL2/SDL_timer.h>
|
#include <SDL2/SDL_timer.h>
|
||||||
#include <bits/pthreadtypes.h>
|
#include <bits/pthreadtypes.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
@@ -24,6 +25,7 @@ int objectCount = 0;
|
|||||||
int objectStorageSize = 0;
|
int objectStorageSize = 0;
|
||||||
MonitorSize_t monitorSize;
|
MonitorSize_t monitorSize;
|
||||||
pthread_t thread;
|
pthread_t thread;
|
||||||
|
pthread_mutex_t mutex;
|
||||||
SDL_Window* win;
|
SDL_Window* win;
|
||||||
|
|
||||||
static int compartSort(const RenderItem_t * a, const RenderItem_t * b) {
|
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 *)
|
//int (*compar)(const void [.size], const void [.size], void *)
|
||||||
static void * render_thread(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
|
// returns zero on success else non-zero
|
||||||
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
|
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
|
||||||
printf("error initializing SDL: %s\n", SDL_GetError());
|
printf("error initializing SDL: %s\n", SDL_GetError());
|
||||||
@@ -64,17 +41,57 @@ void renderer_main() {
|
|||||||
win = SDL_CreateWindow("GAME", // creates a window
|
win = SDL_CreateWindow("GAME", // creates a window
|
||||||
SDL_WINDOWPOS_CENTERED,
|
SDL_WINDOWPOS_CENTERED,
|
||||||
SDL_WINDOWPOS_CENTERED,
|
SDL_WINDOWPOS_CENTERED,
|
||||||
1000, 1000, 0);
|
500, 500, 0);
|
||||||
|
|
||||||
Uint32 render_flags = SDL_RENDERER_ACCELERATED;
|
Uint32 render_flags = SDL_RENDERER_ACCELERATED;
|
||||||
|
|
||||||
// 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);
|
||||||
|
|
||||||
|
if(renderer == NULL) {
|
||||||
|
fprintf(stderr, "Could not create renderer\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_GetRendererOutputSize(renderer, &monitorSize.width, &monitorSize.height);
|
||||||
objectStorageSize = 1000;
|
objectStorageSize = 1000;
|
||||||
objects = malloc(objectStorageSize);
|
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_create(&thread, NULL, render_thread, NULL);
|
||||||
|
pthread_mutex_lock(&mutex);
|
||||||
|
pthread_mutex_destroy(&mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
RenderId_t renderer_renderObject(RenderObject_t object) {
|
RenderId_t renderer_renderObject(RenderObject_t object) {
|
||||||
@@ -83,8 +100,8 @@ RenderId_t renderer_renderObject(RenderObject_t object) {
|
|||||||
SDL_Rect rect = {
|
SDL_Rect rect = {
|
||||||
round(object.x * monitorSize.width),
|
round(object.x * monitorSize.width),
|
||||||
round(object.y * monitorSize.height),
|
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 = {
|
RenderItem_t item = {
|
||||||
|
|||||||
+8
-3
@@ -1,5 +1,6 @@
|
|||||||
#include <SDL2/SDL_error.h>
|
#include <SDL2/SDL_error.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include "renderer.h"
|
||||||
#include "text.h"
|
#include "text.h"
|
||||||
#include <SDL2/SDL_log.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) {
|
if(access(fontFilename, F_OK) != 0) {
|
||||||
fprintf(stderr, "Font file not found: %s\n", fontFilename);
|
fprintf(stderr, "Font file not found: %s\n", fontFilename);
|
||||||
return -1;
|
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);
|
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->x = x;
|
||||||
renderObject->y = y;
|
renderObject->y = y;
|
||||||
renderObject->z = z;
|
renderObject->z = z;
|
||||||
renderObject->width = strlen(text) * fontSize;
|
renderObject->width = w * scale / 500;
|
||||||
renderObject->height = fontSize;
|
renderObject->height = h * scale / 500;
|
||||||
renderObject->angle_degre = 0;
|
renderObject->angle_degre = 0;
|
||||||
renderObject->surface = surface;
|
renderObject->surface = surface;
|
||||||
|
|
||||||
|
|||||||
+1
-2
@@ -12,8 +12,7 @@ typedef struct {
|
|||||||
} Position;
|
} 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);
|
void text_freeFontCache(void);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user