f***ed up and needed to rework packages
This commit is contained in:
parent
b43ab1702f
commit
f7a87d7519
78 changed files with 3713 additions and 0 deletions
14
packages/graphics/glfw/config.c
Normal file
14
packages/graphics/glfw/config.c
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
#ifdef ARC_GLFW_WINDOW
|
||||
|
||||
#include <stdio.h>
|
||||
#include "arc/std/config.h"
|
||||
#include "arc/std/string.h"
|
||||
#include "arc/std/errno.h"
|
||||
|
||||
#include "renderer.h"
|
||||
|
||||
|
||||
void ARC_GraphicsConfig_Init(ARC_Config *config, ARC_Renderer *renderer){
|
||||
}
|
||||
|
||||
#endif // ARC_OPENGL_GRAPHICS
|
||||
52
packages/graphics/glfw/renderer.c
Normal file
52
packages/graphics/glfw/renderer.c
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
#ifdef ARC_GLFW_WINDOW
|
||||
#include "arc/graphics/renderer.h"
|
||||
#include "renderer.h"
|
||||
|
||||
// #ifdef ARC_GLEW
|
||||
#define GLEW_STATIC
|
||||
#include <GL/glew.h>
|
||||
// #endif // ARC_GLEW
|
||||
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
#include "arc/graphics/window.h"
|
||||
#include "arc/std/errno.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
void ARC_Renderer_CreateWithEngineData(ARC_Renderer **renderer, ARC_EngineData *data){
|
||||
if(!info){
|
||||
arc_errno = ARC_ERRNO_NULL;
|
||||
ARC_DEBUG_ERR("ARC_Renderer_Create(**renderer, NULL)");
|
||||
return;
|
||||
}
|
||||
|
||||
// #ifdef ARC_GLEW
|
||||
*renderer = (ARC_Renderer *)malloc(sizeof(ARC_Renderer));
|
||||
(*renderer)->window = (GLFWwindow *)data->window;
|
||||
|
||||
glewExperimental = GL_TRUE;
|
||||
if(glewInit() != GLEW_OK){
|
||||
ARC_DEBUG_ERR("ARC_Renderer_Create(**renderer, info), GLEW failed to init");
|
||||
glfwTerminate();
|
||||
arc_errno = ARC_ERRNO_INIT;
|
||||
}
|
||||
// #endif // ARC_GLEW
|
||||
|
||||
glClearColor(0.23f, 0.38f, 0.47f, 1.0f);
|
||||
}
|
||||
|
||||
void ARC_Renderer_Destroy(ARC_Renderer *renderer){
|
||||
free(renderer);
|
||||
}
|
||||
|
||||
void ARC_Renderer_Clear(ARC_Renderer *renderer){
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
}
|
||||
|
||||
void ARC_Renderer_Render(ARC_Renderer *renderer){
|
||||
// #ifdef ARC_GLEW
|
||||
glfwSwapBuffers(renderer->window);
|
||||
// #endif // ARC_GLEW
|
||||
}
|
||||
|
||||
#endif //ARC_SDL
|
||||
29
packages/graphics/glfw/renderer.h
Normal file
29
packages/graphics/glfw/renderer.h
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
#ifndef ARC_OPENGL_RENDERER_H_
|
||||
#define ARC_OPENGL_RENDERER_H_
|
||||
|
||||
#ifdef ARC_GLFW_WINDOW
|
||||
|
||||
#define GLEW_STATIC
|
||||
#include <GL/glew.h>
|
||||
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
#include "arc/graphics/renderer.h"
|
||||
#include "arc/graphics/window.h"
|
||||
|
||||
typedef struct ARC_RendererType {
|
||||
GLFWwindow *window;
|
||||
} ARC_RendererType;
|
||||
|
||||
/**
|
||||
* @brief struct for info needed to create glfw renderer
|
||||
*
|
||||
* @note this is what needs to be passed into the data parameter for ARC_Renderer_Create
|
||||
*/
|
||||
//struct ARC_RenderInfo {
|
||||
// GLFWwindow *window;
|
||||
//};
|
||||
|
||||
#endif // !ARC_GLFW_WINDOW
|
||||
|
||||
#endif // !ARC_OPENGL_RENDERER_H_
|
||||
36
packages/graphics/glfw/window.c
Normal file
36
packages/graphics/glfw/window.c
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
#ifdef ARC_GLFW_WINDOW
|
||||
#include "arc/graphics/window.h"
|
||||
#include "window.h"
|
||||
|
||||
#include "arc/std/errno.h"
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <stdio.h>
|
||||
|
||||
void framebufferSizeCallback(GLFWwindow *window, int width, int height){
|
||||
glViewport(0, 0, width, height);
|
||||
}
|
||||
|
||||
void ARC_Window_Create(ARC_Window **window, ARC_WindowInfo *info){
|
||||
glfwInit();
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
glfwWindowHint(GLFW_FLOATING, GLFW_TRUE);
|
||||
|
||||
*window = (ARC_Window *) glfwCreateWindow(info->w, info->h, info->title, NULL, NULL);
|
||||
if(*window == NULL){
|
||||
printf("Failed to create GLFW window\n");
|
||||
glfwTerminate();
|
||||
arc_errno = ARC_ERRNO_NULL;
|
||||
}
|
||||
|
||||
glfwMakeContextCurrent((GLFWwindow *)*window);
|
||||
glViewport(0, 0, info->w, info->h);
|
||||
glfwSetFramebufferSizeCallback((GLFWwindow *)*window, framebufferSizeCallback);
|
||||
}
|
||||
|
||||
void ARC_Window_Destroy(ARC_Window *window){
|
||||
glfwTerminate();
|
||||
}
|
||||
|
||||
#endif //ARC_GLFW
|
||||
16
packages/graphics/glfw/window.h
Normal file
16
packages/graphics/glfw/window.h
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
#ifndef ARC_OPENGL_WINDOW_H_
|
||||
#define ARC_OPENGL_WINDOW_H_
|
||||
|
||||
#ifdef ARC_GLFW_WINDOW
|
||||
|
||||
#define GLEW_STATIC
|
||||
#include <GL/glew.h>
|
||||
|
||||
#include "arc/graphics/window.h"
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
typedef GLFWwindow ARC_WindowType;
|
||||
|
||||
#endif // !ARC_GLFW_WINDOW
|
||||
|
||||
#endif // !ARC_GLFW_WINDOW_H_
|
||||
10
packages/graphics/opengl/circle.c
Normal file
10
packages/graphics/opengl/circle.c
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
#ifdef ARC_OPENGL_GRAPHICS
|
||||
|
||||
#include "arc/graphics/circle.h"
|
||||
#include <stdio.h>
|
||||
|
||||
void ARC_Circle_Render(ARC_Circle *circle, ARC_Renderer *renderer, ARC_Color *color){
|
||||
printf("OpenGL Backend Selected\n");
|
||||
}
|
||||
|
||||
#endif // !ARC_OPENGL_GRAPHICS
|
||||
8
packages/graphics/opengl/line.c
Normal file
8
packages/graphics/opengl/line.c
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
#ifdef ARC_OPENGL_GRAPHICS
|
||||
#include "arc/graphics/line.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
void ARC_Line_Render(int32_t *x1, int32_t *y1, int32_t *x2, int32_t *y2, ARC_Renderer *renderer, ARC_Color *color){
|
||||
}
|
||||
|
||||
#endif // ARC_OPENGL_GRAPHICS
|
||||
14
packages/graphics/opengl/obround.c
Normal file
14
packages/graphics/opengl/obround.c
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
#ifdef ARC_OPENGL_GRAPHICS
|
||||
|
||||
#include "arc/graphics/obround.h"
|
||||
#include <stdio.h>
|
||||
|
||||
void ARC_Obround_Render(ARC_Obround *obround, ARC_Renderer *renderer, ARC_Color *color){
|
||||
printf("OpenGL Backend Selected\n");
|
||||
}
|
||||
|
||||
void ARC_FObround_Render(ARC_FObround *obround, ARC_Renderer *renderer, ARC_Color *color){
|
||||
printf("OpenGL Backend Selected\n");
|
||||
}
|
||||
|
||||
#endif // ARC_OPENGL_GRAPHICS
|
||||
11
packages/graphics/opengl/rectangle.c
Normal file
11
packages/graphics/opengl/rectangle.c
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
#ifdef ARC_OPENGL_GRAPHICS
|
||||
#include "arc/graphics/rectangle.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
void ARC_Rect_Render(ARC_Rect *rect, ARC_Renderer *renderer, ARC_Color *color){
|
||||
}
|
||||
|
||||
void ARC_Rect_RenderFill(ARC_Rect *rect, ARC_Renderer *renderer, ARC_Color *color){
|
||||
}
|
||||
|
||||
#endif // ARC_OPENGL_GRAPHICS
|
||||
29
packages/graphics/opengl/sprite.c
Normal file
29
packages/graphics/opengl/sprite.c
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
#ifdef ARC_OPENGL_GRAPHICS
|
||||
#include "arc/graphics/sprite.h"
|
||||
#include "arc/math/point.h"
|
||||
#include "arc/math/rectangle.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
void ARC_Sprite_Create(ARC_Sprite **sprite, ARC_Spritesheet *spritesheet, ARC_Array *frames){
|
||||
}
|
||||
|
||||
void ARC_Sprite_Destroy(ARC_Sprite *sprite){
|
||||
}
|
||||
|
||||
void ARC_Sprite_Copy(ARC_Sprite **newSprite, ARC_Sprite *oldSprite){
|
||||
}
|
||||
|
||||
void ARC_Sprite_Render(ARC_Sprite *sprite, ARC_Renderer *renderer, ARC_Rect *renderBounds){
|
||||
}
|
||||
|
||||
void ARC_Sprite_RenderRotated(ARC_Sprite *sprite, ARC_Renderer *renderer, ARC_Rect *renderBounds, ARC_Point *center, double angle){
|
||||
}
|
||||
|
||||
void ARC_Sprite_IterateFrame(ARC_Sprite *sprite){
|
||||
}
|
||||
|
||||
ARC_Rect *ARC_Sprite_GetBounds(ARC_Sprite *sprite){
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#endif // ARC_OPENGL_GRAPHICS
|
||||
13
packages/graphics/opengl/sprite.h
Normal file
13
packages/graphics/opengl/sprite.h
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#ifndef ARC_OPENGL_SPRITE_H_
|
||||
#define ARC_OPENGL_SPRITE_H_
|
||||
|
||||
#ifdef ARC_OPENGL_GRAPHICS
|
||||
|
||||
#include "arc/graphics/sprite.h"
|
||||
|
||||
struct ARC_Sprite {
|
||||
};
|
||||
|
||||
#endif // !ARC_OPENGL_GRAPHICS
|
||||
|
||||
#endif // !ARC_OPENGL_SPRITE_H_
|
||||
17
packages/graphics/opengl/spritesheet.c
Normal file
17
packages/graphics/opengl/spritesheet.c
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
#ifdef ARC_OPENGL_GRAPHICS
|
||||
#include "arc/graphics/spritesheet.h"
|
||||
#include "arc/math/point.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
void ARC_Spritesheet_RenderArea(ARC_Spritesheet *spritesheet, ARC_Rect *sheetBounds, ARC_Renderer *renderer, ARC_Rect *renderBounds){
|
||||
}
|
||||
|
||||
ARC_Point ARC_Spritesheet_GetSize(ARC_Spritesheet *spritesheet){
|
||||
return (ARC_Point){0, 0};
|
||||
}
|
||||
|
||||
uint32_t *ARC_Spritesheet_GetTileSize(ARC_Spritesheet *spritesheet){
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#endif // ARC_OPENGL_GRAPHICS
|
||||
13
packages/graphics/opengl/spritesheet.h
Normal file
13
packages/graphics/opengl/spritesheet.h
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#ifndef ARC_OPENGL_SPRITESHEET_H_
|
||||
#define ARC_OPENGL_SPRITESHEET_H_
|
||||
|
||||
#ifdef ARC_OPENGL_GRAPHICS
|
||||
|
||||
#include "arc/graphics/spritesheet.h"
|
||||
|
||||
struct ARC_Spritesheet {
|
||||
};
|
||||
|
||||
#endif // !ARC_OPENGL_GRAPHICS
|
||||
|
||||
#endif // !ARC_OPENGL_SPRITESHEET_H_
|
||||
26
packages/graphics/opengl/text.c
Normal file
26
packages/graphics/opengl/text.c
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
#ifdef ARC_OPENGL_GRAPHICS
|
||||
|
||||
#include "arc/graphics/text.h"
|
||||
#include <stdio.h>
|
||||
|
||||
void ARC_Text_Create(ARC_Text **text, ARC_String *path, int32_t size, ARC_Color color){
|
||||
printf("OpenGL Backend Selected\n");
|
||||
}
|
||||
|
||||
void ARC_Text_Destroy(ARC_Text *font){
|
||||
printf("OpenGL Backend Selected\n");
|
||||
}
|
||||
|
||||
void ARC_Text_SetString(ARC_Text *text, ARC_Renderer *renderer, ARC_String *string){
|
||||
printf("OpenGL Backend Selected\n");
|
||||
}
|
||||
|
||||
void ARC_Text_Render(ARC_Text *text, ARC_Renderer *renderer){
|
||||
printf("OpenGL Backend Selected\n");
|
||||
}
|
||||
|
||||
void ARC_Text_SetPos(ARC_Text *text, ARC_Point pos){
|
||||
printf("OpenGL Backend Selected\n");
|
||||
}
|
||||
|
||||
#endif // !ARC_OPENGL_GRAPHICS
|
||||
14
packages/graphics/opengl/text.h
Normal file
14
packages/graphics/opengl/text.h
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
#ifndef ARC_OPENGL_TEXT_H_
|
||||
#define ARC_OPENGL_TEXT_H_
|
||||
|
||||
#include "arc/std/string.h"
|
||||
#include "arc/graphics/color.h"
|
||||
#include "arc/math/rectangle.h"
|
||||
|
||||
#ifdef ARC_OPENGL_GRAPHICS
|
||||
|
||||
typedef struct ARC_Text {} ARC_Text;
|
||||
|
||||
#endif // !ARC_OPENGL_GRAPHICS
|
||||
|
||||
#endif // !ARC_OPENGL_TEXT_H_
|
||||
49
packages/graphics/sdl/circle.c
Normal file
49
packages/graphics/sdl/circle.c
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
#include "arc/graphics/circle.h"
|
||||
#include "renderer.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
//Modified from https://stackoverflow.com/questions/38334081/how-to-draw-circles-arcs-and-vector-graphics-in-sdl
|
||||
void ARC_Circle_Render(ARC_Circle *circle, ARC_Renderer *renderer, ARC_Color *color){
|
||||
SDL_SetRenderDrawColor((SDL_Renderer *)renderer, color->r, color->g, color->b, color->a);
|
||||
|
||||
int32_t diameter = (circle->r * 2);
|
||||
|
||||
int32_t x = (circle->r - 1);
|
||||
int32_t y = 0;
|
||||
int32_t tx = 1;
|
||||
int32_t ty = 1;
|
||||
int32_t error = (tx - diameter);
|
||||
|
||||
while(x >= y){
|
||||
// Each of the following renders an octant of the circle
|
||||
SDL_RenderDrawPoint((SDL_Renderer *)renderer, circle->x + x, circle->y - y);
|
||||
SDL_RenderDrawPoint((SDL_Renderer *)renderer, circle->x + x, circle->y + y);
|
||||
SDL_RenderDrawPoint((SDL_Renderer *)renderer, circle->x - x, circle->y - y);
|
||||
SDL_RenderDrawPoint((SDL_Renderer *)renderer, circle->x - x, circle->y + y);
|
||||
SDL_RenderDrawPoint((SDL_Renderer *)renderer, circle->x + y, circle->y - x);
|
||||
SDL_RenderDrawPoint((SDL_Renderer *)renderer, circle->x + y, circle->y + x);
|
||||
SDL_RenderDrawPoint((SDL_Renderer *)renderer, circle->x - y, circle->y - x);
|
||||
SDL_RenderDrawPoint((SDL_Renderer *)renderer, circle->x - y, circle->y + x);
|
||||
|
||||
if(error <= 0){
|
||||
++y;
|
||||
error += ty;
|
||||
ty += 2;
|
||||
}
|
||||
|
||||
if(error > 0){
|
||||
--x;
|
||||
tx += 2;
|
||||
error += (tx - diameter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//TODO: very temp
|
||||
void ARC_Circle_RenderFill(ARC_Circle *circle, ARC_Renderer *renderer, ARC_Color *color){
|
||||
ARC_Circle temp = *circle;
|
||||
|
||||
for(; temp.r; temp.r--){
|
||||
ARC_Circle_Render(&temp, renderer, color);
|
||||
}
|
||||
}
|
||||
311
packages/graphics/sdl/config.c
Normal file
311
packages/graphics/sdl/config.c
Normal file
|
|
@ -0,0 +1,311 @@
|
|||
#include "arc/graphics/config.h"
|
||||
|
||||
#include <SDL_image.h>
|
||||
#include <stdio.h>
|
||||
#include "renderer.h"
|
||||
#include "sprite.h"
|
||||
#include "spritesheet.h"
|
||||
#include "arc/std/array.h"
|
||||
#include "arc/std/string.h"
|
||||
#include "arc/std/errno.h"
|
||||
#include "arc/graphics/sprite.h"
|
||||
#include "arc/graphics/spritesheet.h"
|
||||
#include "arc/math/config.h"
|
||||
#include "arc/math/point.h"
|
||||
#include "arc/math/rectangle.h"
|
||||
|
||||
// #define ARC_DEFAULT_CONFIG
|
||||
#include "arc/std/defaults/config.h"
|
||||
|
||||
SDL_Renderer *global_renderer;
|
||||
|
||||
uint8_t ARC_SDL_Texture_Read(ARC_Config *config, ARC_String *string, void **value);
|
||||
uint8_t ARC_Spritesheet_Read(ARC_Config *config, ARC_String *string, void **value);
|
||||
uint8_t ARC_Sprite_Read (ARC_Config *config, ARC_String *string, void **value);
|
||||
|
||||
void ARC_SDL_Texture_Delete(ARC_Config *config, ARC_String *string, void *value);
|
||||
void ARC_Spritesheet_Delete(ARC_Config *config, ARC_String *string, void *value);
|
||||
void ARC_Sprite_Delete (ARC_Config *config, ARC_String *string, void *value);
|
||||
|
||||
void ARC_GraphicsConfig_Init(ARC_Config *config, ARC_Renderer *renderer){
|
||||
global_renderer = (SDL_Renderer *)renderer;
|
||||
ARC_Config_AddKeyCString(config, (char *)"SDL_Texture" , 11, ARC_SDL_Texture_Read, ARC_SDL_Texture_Delete);
|
||||
ARC_Config_AddKeyCString(config, (char *)"ARC_Spritesheet", 15, ARC_Spritesheet_Read, ARC_Spritesheet_Delete);
|
||||
ARC_Config_AddKeyCString(config, (char *)"ARC_Sprite" , 10, ARC_Sprite_Read , ARC_Sprite_Delete );
|
||||
}
|
||||
|
||||
uint64_t ARC_GraphicsConfig_GetIndexAndErrorCheck(ARC_String *string, char *search, uint64_t searchLength){
|
||||
uint64_t separator = ARC_String_FindCString(string, ",", 1);
|
||||
|
||||
if(separator == ~(uint64_t)0){
|
||||
arc_errno = ARC_ERRNO_DATA;
|
||||
}
|
||||
|
||||
return separator;
|
||||
}
|
||||
|
||||
int32_t ARC_SDL_Texture_Load(const char *path, SDL_Texture **texture){
|
||||
IMG_Init(IMG_INIT_PNG);
|
||||
SDL_Surface *surface = IMG_Load(path);
|
||||
if(!surface){
|
||||
printf("Error: reading png '%s'\nSDL_Image Error: %s", path, IMG_GetError());
|
||||
return 1; // GE_SDL_ERRNO_
|
||||
}
|
||||
|
||||
SDL_BlendMode tempMode;
|
||||
SDL_GetSurfaceBlendMode(surface, &tempMode);
|
||||
*texture = SDL_CreateTextureFromSurface(global_renderer, surface);
|
||||
SDL_GetTextureBlendMode(*texture, &tempMode);
|
||||
|
||||
SDL_FreeSurface(surface);
|
||||
IMG_Quit();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t ARC_SDL_Texture_Read(ARC_Config* config, ARC_String *string, void **value){
|
||||
ARC_Config_Get(config, string, value);
|
||||
if(*value){
|
||||
return 1;
|
||||
}
|
||||
|
||||
ARC_String *tempStr, *textureStr;
|
||||
ARC_String_StripEndsWhitespace(&tempStr, string);
|
||||
|
||||
ARC_String_CopySubstring(&textureStr, tempStr, 1, tempStr->length - 2);
|
||||
ARC_String_Destroy(tempStr);
|
||||
|
||||
ARC_SDL_Texture_Load(textureStr->data, (SDL_Texture **)value);
|
||||
|
||||
ARC_String_Destroy(textureStr);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ARC_Spritesheet_ReadTexture(ARC_Config *config, ARC_String *string, uint32_t *size, void **value){
|
||||
SDL_Texture *texture;
|
||||
|
||||
ARC_String *tempStr, *textureStr;
|
||||
ARC_String_StripEndsWhitespace(&tempStr, string);
|
||||
|
||||
//check for reference
|
||||
ARC_Config_Get(config, tempStr, (void **)&texture);
|
||||
if(!texture && (tempStr->data[0] != '"' || tempStr->data[string->length - 1] != '"')){
|
||||
arc_errno = ARC_ERRNO_DATA;
|
||||
}
|
||||
|
||||
ARC_String_CopySubstring(&textureStr, tempStr, 1, tempStr->length - 2);
|
||||
ARC_String_Destroy(tempStr);
|
||||
|
||||
//try reading in the texture
|
||||
if(!texture){
|
||||
ARC_SDL_Texture_Read(config, string, (void **)&texture);
|
||||
if(arc_errno){
|
||||
*value = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
ARC_String_Destroy(textureStr);
|
||||
|
||||
*value = malloc(sizeof(ARC_Spritesheet));
|
||||
((ARC_Spritesheet *) *value)->texture = texture;
|
||||
((ARC_Spritesheet *) *value)->size = size;
|
||||
}
|
||||
|
||||
uint8_t ARC_Spritesheet_Read(ARC_Config* config, ARC_String *string, void **value){
|
||||
if(string->data[0] != '{' || string->data[string->length - 1] != '}'){
|
||||
ARC_Spritesheet_ReadTexture(config, string, NULL, value);
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint64_t split = ARC_GraphicsConfig_GetIndexAndErrorCheck(string, ",", 1);
|
||||
if(arc_errno){
|
||||
return 0;
|
||||
}
|
||||
|
||||
ARC_String *temp, *textureStr, *sizeStr;
|
||||
ARC_String_CopySubstring(&temp, string, 1, split - 2);
|
||||
ARC_String_StripEndsWhitespace(&textureStr, temp);
|
||||
ARC_String_Destroy(temp);
|
||||
|
||||
ARC_String_CopySubstring(&temp, string, split + 1, string->length - (split + 1));
|
||||
ARC_String_StripEndsWhitespace(&sizeStr, temp);
|
||||
ARC_String_Destroy(temp);
|
||||
|
||||
uint32_t *size;
|
||||
ARC_Config_Get(config, string, (void **)&size);
|
||||
if(!size){
|
||||
ARC_ConfigKey_Read_Uint32_t(config, sizeStr, (void **)&size);
|
||||
if(arc_errno){
|
||||
ARC_String_Destroy(sizeStr);
|
||||
ARC_String_Destroy(textureStr);
|
||||
return ARC_ERRNO_DATA;
|
||||
}
|
||||
}
|
||||
|
||||
ARC_Spritesheet_ReadTexture(config, textureStr, size, value);
|
||||
|
||||
ARC_String_Destroy(sizeStr);
|
||||
ARC_String_Destroy(textureStr);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t ARC_Sprite_Read(ARC_Config* config, ARC_String *string, void **value){
|
||||
ARC_Config_Get(config, string, value);
|
||||
if(*value){
|
||||
return 1;
|
||||
}
|
||||
|
||||
if(string->data[0] != '{' || string->data[string->length - 1] != '}'){
|
||||
ARC_Spritesheet_ReadTexture(config, string, NULL, value);
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint64_t split = ARC_GraphicsConfig_GetIndexAndErrorCheck(string, ",", 1);
|
||||
if(arc_errno){
|
||||
return 0;
|
||||
}
|
||||
|
||||
ARC_String *temp, *spritesheetStr, *framesStr;
|
||||
ARC_String_CopySubstring(&temp, string, 1, split - 2);
|
||||
ARC_String_StripEndsWhitespace(&spritesheetStr, temp);
|
||||
ARC_String_Destroy(temp);
|
||||
|
||||
ARC_String_CopySubstring(&temp, string, split + 1, string->length - (split + 2));
|
||||
ARC_String_StripEndsWhitespace(&framesStr, temp);
|
||||
ARC_String_Destroy(temp);
|
||||
|
||||
//spritesheet
|
||||
ARC_Spritesheet *spritesheet;
|
||||
ARC_Config_Get(config, spritesheetStr, (void **)&spritesheet);
|
||||
|
||||
if(!spritesheet){
|
||||
ARC_Spritesheet_Read(config, spritesheetStr, (void **)&spritesheet);
|
||||
if(arc_errno){
|
||||
ARC_String_Destroy(spritesheetStr);
|
||||
ARC_String_Destroy(framesStr );
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
//bounds
|
||||
ARC_Array *frames;
|
||||
ARC_Config_Get(config, framesStr, (void **)&frames);
|
||||
|
||||
if(!frames){
|
||||
ARC_RectArray_Read(config, framesStr, (void **)&frames);
|
||||
if(arc_errno){
|
||||
ARC_String_Destroy(spritesheetStr);
|
||||
ARC_String_Destroy(framesStr );
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
ARC_String_Destroy(spritesheetStr);
|
||||
ARC_String_Destroy(framesStr );
|
||||
|
||||
// Scale frames to match spritesheet size
|
||||
// TODO: possible bug for sheets that use same frames
|
||||
if(spritesheet->size){
|
||||
for(uint32_t i = 0; i < frames->size; i++){
|
||||
((ARC_Rect *)frames->data)[i].x *= *spritesheet->size;
|
||||
((ARC_Rect *)frames->data)[i].y *= *spritesheet->size;
|
||||
((ARC_Rect *)frames->data)[i].w *= *spritesheet->size;
|
||||
((ARC_Rect *)frames->data)[i].h *= *spritesheet->size;
|
||||
}
|
||||
}
|
||||
//sprite
|
||||
ARC_Sprite_Create((ARC_Sprite **)value, spritesheet, frames);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ARC_SDL_Texture_Delete(ARC_Config* config, ARC_String *string, void *value){
|
||||
SDL_DestroyTexture((SDL_Texture *) value);
|
||||
}
|
||||
|
||||
void ARC_Spritesheet_Delete(ARC_Config* config, ARC_String *string, void *value){
|
||||
ARC_Spritesheet *sheetValue = (ARC_Spritesheet *)value;
|
||||
|
||||
//check if read in as a Textrue reference
|
||||
void *temp;
|
||||
ARC_Config_Get(config, string, &temp);
|
||||
if(temp){
|
||||
//TODO: test to see if this breaks references
|
||||
free(sheetValue);
|
||||
return;
|
||||
}
|
||||
|
||||
uint64_t split = ARC_GraphicsConfig_GetIndexAndErrorCheck(string, ",", 1);
|
||||
if(arc_errno){
|
||||
//TODO: test to make sure no edge cases
|
||||
// free(sheetValue);
|
||||
ARC_SDL_Texture_Delete(config, string, value);
|
||||
arc_errno = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
if(split == ~0){
|
||||
|
||||
}
|
||||
|
||||
//check if texture and size are references
|
||||
ARC_String *tempStr, *textureStr, *sizeStr;
|
||||
ARC_String_CopySubstring(&tempStr, string, 1, split - 1);
|
||||
ARC_String_StripEndsWhitespace(&textureStr, tempStr);
|
||||
ARC_String_Destroy(tempStr);
|
||||
|
||||
ARC_String_CopySubstring(&tempStr, string, split + 1, string->length - (split + 1));
|
||||
ARC_String_StripEndsWhitespace(&sizeStr, tempStr);
|
||||
ARC_String_Destroy(tempStr);
|
||||
|
||||
ARC_Config_Get(config, sizeStr, (void **)&temp);
|
||||
ARC_String_Destroy(sizeStr);
|
||||
if(temp){
|
||||
free(sheetValue->size);
|
||||
}
|
||||
|
||||
ARC_Config_Get(config, textureStr, (void **)&temp);
|
||||
ARC_String_Destroy(textureStr);
|
||||
if(temp){
|
||||
free(sheetValue->size);
|
||||
}
|
||||
|
||||
free(sheetValue);
|
||||
}
|
||||
|
||||
void ARC_Sprite_Delete(ARC_Config* config, ARC_String *string, void *value){
|
||||
ARC_Sprite *spriteValue = (ARC_Sprite *)value;
|
||||
|
||||
//check if read in as a Textrue reference
|
||||
void *temp;
|
||||
uint64_t split = ARC_GraphicsConfig_GetIndexAndErrorCheck(string, ",", 1);
|
||||
if(arc_errno){
|
||||
free(spriteValue);
|
||||
return;
|
||||
}
|
||||
|
||||
//check if texture and size are references
|
||||
ARC_String *tempStr, *spritesheetStr, *framesStr;
|
||||
ARC_String_CopySubstring(&tempStr, string, 1, split - 1);
|
||||
ARC_String_StripEndsWhitespace(&spritesheetStr, tempStr);
|
||||
ARC_String_Destroy(tempStr);
|
||||
|
||||
ARC_String_CopySubstring(&tempStr, string, split + 1, string->length - (split + 1));
|
||||
ARC_String_StripEndsWhitespace(&framesStr, tempStr);
|
||||
ARC_String_Destroy(tempStr);
|
||||
|
||||
ARC_Config_Get(config, spritesheetStr, (void **)&temp);
|
||||
ARC_String_Destroy(spritesheetStr);
|
||||
if(temp){
|
||||
free(spriteValue->spritesheet);
|
||||
}
|
||||
|
||||
ARC_Config_Get(config, framesStr, (void **)&temp);
|
||||
ARC_String_Destroy(framesStr);
|
||||
if(temp){
|
||||
free(spriteValue->frames);
|
||||
}
|
||||
|
||||
free(spriteValue);
|
||||
}
|
||||
8
packages/graphics/sdl/line.c
Normal file
8
packages/graphics/sdl/line.c
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
#include "arc/graphics/line.h"
|
||||
#include "renderer.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
void ARC_Line_Render(int32_t *x1, int32_t *y1, int32_t *x2, int32_t *y2, ARC_Renderer *renderer, ARC_Color *color){
|
||||
SDL_SetRenderDrawColor((SDL_Renderer *)renderer, color->r, color->g, color->b, color->a);
|
||||
SDL_RenderDrawLine((SDL_Renderer *)renderer, *x1, *y1, *x2, *y2);
|
||||
}
|
||||
48
packages/graphics/sdl/obround.c
Normal file
48
packages/graphics/sdl/obround.c
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
#include "arc/graphics/obround.h"
|
||||
#include "renderer.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
//Modified from https://stackoverflow.com/questions/38334081/how-to-draw-circles-arcs-and-vector-graphics-in-sdl
|
||||
void ARC_Obround_Render(ARC_Obround *obround, ARC_Renderer *renderer, ARC_Color *color){
|
||||
SDL_SetRenderDrawColor((SDL_Renderer *)renderer, color->r, color->g, color->b, color->a);
|
||||
|
||||
int32_t diameter = (obround->r * 2);
|
||||
|
||||
int32_t x = (obround->r - 1);
|
||||
int32_t y = 0;
|
||||
int32_t tx = 1;
|
||||
int32_t ty = 1;
|
||||
int32_t error = (tx - diameter);
|
||||
|
||||
SDL_RenderDrawLine((SDL_Renderer *)renderer, obround->x - obround->r, obround->y - (obround->h / 2), obround->x - obround->r, obround->y + (obround->h / 2));
|
||||
SDL_RenderDrawLine((SDL_Renderer *)renderer, obround->x + obround->r, obround->y - (obround->h / 2), obround->x + obround->r, obround->y + (obround->h / 2));
|
||||
|
||||
while(x >= y){
|
||||
// Each of the following renders an octant of the circle
|
||||
SDL_RenderDrawPoint((SDL_Renderer *)renderer, obround->x + x, obround->y - y - (obround->h / 2));
|
||||
SDL_RenderDrawPoint((SDL_Renderer *)renderer, obround->x + x, obround->y + y + (obround->h / 2));
|
||||
SDL_RenderDrawPoint((SDL_Renderer *)renderer, obround->x - x, obround->y - y - (obround->h / 2));
|
||||
SDL_RenderDrawPoint((SDL_Renderer *)renderer, obround->x - x, obround->y + y + (obround->h / 2));
|
||||
SDL_RenderDrawPoint((SDL_Renderer *)renderer, obround->x + y, obround->y - x - (obround->h / 2));
|
||||
SDL_RenderDrawPoint((SDL_Renderer *)renderer, obround->x + y, obround->y + x + (obround->h / 2));
|
||||
SDL_RenderDrawPoint((SDL_Renderer *)renderer, obround->x - y, obround->y - x - (obround->h / 2));
|
||||
SDL_RenderDrawPoint((SDL_Renderer *)renderer, obround->x - y, obround->y + x + (obround->h / 2));
|
||||
|
||||
if(error <= 0){
|
||||
++y;
|
||||
error += ty;
|
||||
ty += 2;
|
||||
}
|
||||
|
||||
if(error > 0){
|
||||
--x;
|
||||
tx += 2;
|
||||
error += (tx - diameter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ARC_FObround_Render(ARC_FObround *obround, ARC_Renderer *renderer, ARC_Color *color){
|
||||
ARC_Obround casted = ARC_FObround_CastToObround(obround);
|
||||
ARC_Obround_Render(&casted, renderer, color);
|
||||
}
|
||||
23
packages/graphics/sdl/rectangle.c
Normal file
23
packages/graphics/sdl/rectangle.c
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
#include "arc/graphics/rectangle.h"
|
||||
#include "renderer.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
void ARC_Rect_Render(ARC_Rect *rect, ARC_Renderer *renderer, ARC_Color *color){
|
||||
SDL_SetRenderDrawColor((SDL_Renderer *)renderer, color->r, color->g, color->b, color->a);
|
||||
SDL_RenderDrawRect((SDL_Renderer *)renderer, (SDL_Rect *) rect);
|
||||
}
|
||||
|
||||
void ARC_Rect_RenderFill(ARC_Rect *rect, ARC_Renderer *renderer, ARC_Color *color){
|
||||
SDL_SetRenderDrawColor((SDL_Renderer *)renderer, color->r, color->g, color->b, color->a);
|
||||
SDL_RenderFillRect((SDL_Renderer *)renderer, (SDL_Rect *) rect);
|
||||
}
|
||||
|
||||
void ARC_FRect_Render(ARC_FRect *rect, ARC_Renderer *renderer, ARC_Color *color){
|
||||
ARC_Rect casted = ARC_FRect_CastToRect(rect);
|
||||
ARC_Rect_Render(&casted, renderer, color);
|
||||
}
|
||||
|
||||
void ARC_FRect_RenderFill(ARC_FRect *rect, ARC_Renderer *renderer, ARC_Color *color){
|
||||
ARC_Rect casted = ARC_FRect_CastToRect(rect);
|
||||
ARC_Rect_RenderFill(&casted, renderer, color);
|
||||
}
|
||||
39
packages/graphics/sdl/renderer.c
Normal file
39
packages/graphics/sdl/renderer.c
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
#include "arc/graphics/renderer.h"
|
||||
#include "renderer.h"
|
||||
|
||||
#include <SDL.h>
|
||||
#include <stdlib.h>
|
||||
#include "arc/engine/engine.h"
|
||||
#include "arc/graphics/window.h"
|
||||
#include "arc/std/errno.h"
|
||||
|
||||
void ARC_Renderer_CreateWithEngineData(ARC_Renderer **renderer, ARC_EngineData *data){
|
||||
if(!data){
|
||||
arc_errno = ARC_ERRNO_NULL;
|
||||
ARC_DEBUG_ERR("ARC_Renderer_CreateWithEngineData(**renderer, NULL)");
|
||||
return;
|
||||
}
|
||||
|
||||
*renderer = (ARC_Renderer *)SDL_CreateRenderer((SDL_Window *)(data->window), -1, SDL_RENDERER_ACCELERATED);
|
||||
|
||||
if(!*renderer){
|
||||
arc_errno = ARC_ERRNO_NULL;
|
||||
ARC_DEBUG_LOG(arc_errno, "SDL_CreateRenderer(%p, %d, %u);", data->window, -1, SDL_RENDERER_ACCELERATED);
|
||||
free(renderer);
|
||||
}
|
||||
|
||||
SDL_SetRenderDrawBlendMode((SDL_Renderer *)*renderer, SDL_BLENDMODE_BLEND);
|
||||
}
|
||||
|
||||
void ARC_Renderer_Destroy(ARC_Renderer *renderer){
|
||||
SDL_DestroyRenderer((SDL_Renderer *) renderer);
|
||||
}
|
||||
|
||||
void ARC_Renderer_Clear(ARC_Renderer *renderer){
|
||||
SDL_SetRenderDrawColor((SDL_Renderer *)renderer, 0x1c, 0x2c, 0x3c, 0x00);
|
||||
SDL_RenderClear((SDL_Renderer *)renderer);
|
||||
}
|
||||
|
||||
void ARC_Renderer_Render(ARC_Renderer *renderer){
|
||||
SDL_RenderPresent((SDL_Renderer *)renderer);
|
||||
}
|
||||
10
packages/graphics/sdl/renderer.h
Normal file
10
packages/graphics/sdl/renderer.h
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
#ifndef ARC_SDL_RENDERER_H_
|
||||
#define ARC_SDL_RENDERER_H_
|
||||
|
||||
#include "arc/graphics/renderer.h"
|
||||
#include "arc/graphics/window.h"
|
||||
#include <SDL.h>
|
||||
|
||||
typedef SDL_Renderer ARC_RendererType;
|
||||
|
||||
#endif // !ARC_SDL_RENDERER_H_
|
||||
90
packages/graphics/sdl/sprite.c
Normal file
90
packages/graphics/sdl/sprite.c
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
#include "arc/graphics/sprite.h"
|
||||
|
||||
#include "sprite.h"
|
||||
#include "spritesheet.h"
|
||||
#include "renderer.h"
|
||||
#include "arc/math/point.h"
|
||||
#include "arc/math/rectangle.h"
|
||||
#include "arc/std/errno.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
void ARC_Sprite_Create(ARC_Sprite **sprite, ARC_Spritesheet *spritesheet, ARC_Array *frames){
|
||||
*sprite = (ARC_Sprite *)malloc(sizeof(ARC_Sprite));
|
||||
(*sprite)->spritesheet = spritesheet;
|
||||
(*sprite)->frames = frames;
|
||||
(*sprite)->frameIndex = (uint32_t *)malloc(sizeof(uint32_t));
|
||||
*(*sprite)->frameIndex = 0;
|
||||
(*sprite)->opacity = 255;
|
||||
}
|
||||
|
||||
void ARC_Sprite_Destroy(ARC_Sprite *sprite){
|
||||
free(sprite);
|
||||
}
|
||||
|
||||
void ARC_Sprite_Copy(ARC_Sprite **newSprite, ARC_Sprite *oldSprite){
|
||||
*newSprite = (ARC_Sprite *)malloc(sizeof(ARC_Sprite));
|
||||
(*newSprite)->spritesheet = oldSprite->spritesheet;
|
||||
(*newSprite)->frames = oldSprite->frames;
|
||||
(*newSprite)->frameIndex = (uint32_t *)malloc(sizeof(uint32_t));
|
||||
*(*newSprite)->frameIndex = *oldSprite->frameIndex;
|
||||
}
|
||||
|
||||
void ARC_Sprite_SetOpacity(ARC_Sprite *sprite, uint8_t opacity){
|
||||
sprite->opacity = opacity;
|
||||
}
|
||||
|
||||
void ARC_Sprite_Render(ARC_Sprite *sprite, ARC_Renderer *renderer, ARC_Rect *renderBounds){
|
||||
//TODO: note, this is set here so not all entities in the sheet get opacity set
|
||||
SDL_SetTextureAlphaMod((SDL_Texture *)sprite->spritesheet->texture, sprite->opacity);
|
||||
SDL_RenderCopy((SDL_Renderer *)renderer, sprite->spritesheet->texture, (SDL_Rect *)sprite->frames->data + *sprite->frameIndex, (SDL_Rect *)renderBounds);
|
||||
}
|
||||
|
||||
void ARC_Sprite_RenderFlip(ARC_Sprite *sprite, ARC_Renderer *renderer, ARC_Rect *renderBounds, enum ARC_Sprite_Axis axis){
|
||||
SDL_RendererFlip flip = SDL_FLIP_NONE;
|
||||
if(axis & ARC_SPRITE_AXIS_X){
|
||||
flip |= SDL_FLIP_HORIZONTAL;
|
||||
}
|
||||
|
||||
if(axis & ARC_SPRITE_AXIS_Y){
|
||||
flip |= SDL_FLIP_VERTICAL;
|
||||
}
|
||||
|
||||
//TODO: note, this is set here so not all entities in the sheet get opacity set
|
||||
SDL_SetTextureAlphaMod((SDL_Texture *)sprite->spritesheet->texture, sprite->opacity);
|
||||
SDL_RenderCopyEx((SDL_Renderer *)renderer, sprite->spritesheet->texture, (SDL_Rect *)sprite->frames->data + *sprite->frameIndex, (SDL_Rect *)renderBounds, 0.0, NULL, flip);
|
||||
}
|
||||
|
||||
void ARC_Sprite_RenderRotated(ARC_Sprite *sprite, ARC_Renderer *renderer, ARC_Rect *renderBounds, ARC_Point *center, double angle){
|
||||
//TODO: note, this is set here so not all entities in the sheet get opacity set
|
||||
SDL_SetTextureAlphaMod((SDL_Texture *)sprite->spritesheet->texture, sprite->opacity);
|
||||
SDL_RenderCopyEx((SDL_Renderer *)renderer, sprite->spritesheet->texture, (SDL_Rect *)sprite->frames->data + *sprite->frameIndex, (SDL_Rect *)renderBounds, angle, (SDL_Point *)center, SDL_FLIP_NONE);
|
||||
}
|
||||
|
||||
void ARC_Sprite_SetFrameIndex(ARC_Sprite *sprite, uint32_t index){
|
||||
if(sprite->frames->size <= index){
|
||||
arc_errno = ARC_ERRNO_DATA;
|
||||
ARC_DEBUG_LOG(arc_errno, "in ARC_Sprite_SetFrameIndex(sprite, %d); index out of bounds", index);
|
||||
return;
|
||||
}
|
||||
*sprite->frameIndex = index;
|
||||
}
|
||||
|
||||
void ARC_Sprite_IterateFrame(ARC_Sprite *sprite){
|
||||
++*sprite->frameIndex;
|
||||
|
||||
if(*sprite->frameIndex == sprite->frames->size){
|
||||
*sprite->frameIndex = 0;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t ARC_Sprite_GetFrameIndex(ARC_Sprite *sprite){
|
||||
return *sprite->frameIndex;
|
||||
}
|
||||
|
||||
ARC_Rect *ARC_Sprite_GetBounds(ARC_Sprite *sprite){
|
||||
return (ARC_Rect *)sprite->frames->data + *sprite->frameIndex;
|
||||
}
|
||||
|
||||
ARC_Array *ARC_Sprite_GetAllBounds(ARC_Sprite *sprite){
|
||||
return sprite->frames;
|
||||
}
|
||||
15
packages/graphics/sdl/sprite.h
Normal file
15
packages/graphics/sdl/sprite.h
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
#ifndef ARC_SDL_SPRITE_H_
|
||||
#define ARC_SDL_SPRITE_H_
|
||||
|
||||
#include "arc/graphics/sprite.h"
|
||||
#include <SDL.h>
|
||||
|
||||
struct ARC_Sprite {
|
||||
ARC_Spritesheet *spritesheet;
|
||||
ARC_Array *frames;
|
||||
uint32_t *frameIndex;
|
||||
//TODO: temp
|
||||
uint8_t opacity;
|
||||
};
|
||||
|
||||
#endif // !ARC_SDL_SPRITE_H_
|
||||
20
packages/graphics/sdl/spritesheet.c
Normal file
20
packages/graphics/sdl/spritesheet.c
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
#include "arc/graphics/spritesheet.h"
|
||||
|
||||
#include "spritesheet.h"
|
||||
#include "renderer.h"
|
||||
#include "arc/math/point.h"
|
||||
#include <SDL_render.h>
|
||||
|
||||
void ARC_Spritesheet_RenderArea(ARC_Spritesheet *spritesheet, ARC_Rect *sheetBounds, ARC_Renderer *renderer, ARC_Rect *renderBounds){
|
||||
SDL_RenderCopy((SDL_Renderer *)renderer, spritesheet->texture, (SDL_Rect *)sheetBounds, (SDL_Rect *)renderBounds);
|
||||
}
|
||||
|
||||
ARC_Point ARC_Spritesheet_GetSize(ARC_Spritesheet *spritesheet){
|
||||
ARC_Point size;
|
||||
SDL_QueryTexture(spritesheet->texture, NULL, NULL, &size.x, &size.y);
|
||||
return size;
|
||||
}
|
||||
|
||||
uint32_t *ARC_Spritesheet_GetTileSize(ARC_Spritesheet *spritesheet){
|
||||
return spritesheet->size;
|
||||
}
|
||||
12
packages/graphics/sdl/spritesheet.h
Normal file
12
packages/graphics/sdl/spritesheet.h
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
#ifndef ARC_SDL_SPRITESHEET_H_
|
||||
#define ARC_SDL_SPRITESHEET_H_
|
||||
|
||||
#include "arc/graphics/spritesheet.h"
|
||||
#include <SDL.h>
|
||||
|
||||
struct ARC_Spritesheet {
|
||||
SDL_Texture *texture;
|
||||
uint32_t *size;
|
||||
};
|
||||
|
||||
#endif // !ARC_SDL_SPRITESHEET_H_
|
||||
57
packages/graphics/sdl/text.c
Normal file
57
packages/graphics/sdl/text.c
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
#include "arc/graphics/text.h"
|
||||
|
||||
#include "text.h"
|
||||
#include "arc/graphics/color.h"
|
||||
#include "arc/math/point.h"
|
||||
#include "arc/math/rectangle.h"
|
||||
#include "arc/std/string.h"
|
||||
|
||||
#include <SDL2/SDL_ttf.h>
|
||||
|
||||
void ARC_Text_Create(ARC_Text **text, ARC_String *path, int32_t size, ARC_Color color){
|
||||
*text = (ARC_Text *)malloc(sizeof(ARC_Text));
|
||||
ARC_String_Copy(&(*text)->name, path);
|
||||
(*text)->size = size;
|
||||
(*text)->color = color;
|
||||
(*text)->texture = NULL;
|
||||
(*text)->bounds = (ARC_Rect){ 0, 0, 0, 0 };
|
||||
}
|
||||
|
||||
void ARC_Text_Destroy(ARC_Text *font){
|
||||
if(font->texture != NULL){
|
||||
SDL_DestroyTexture(font->texture);
|
||||
}
|
||||
ARC_String_Destroy(font->name);
|
||||
free(font);
|
||||
}
|
||||
|
||||
void ARC_Text_SetString(ARC_Text *text, ARC_Renderer *renderer, ARC_String *string){
|
||||
TTF_Font *ttfont = TTF_OpenFont(text->name->data, text->size);
|
||||
SDL_Color textColor = (SDL_Color){ text->color.r, text->color.g, text->color.b, text->color.a };
|
||||
SDL_Surface *surface = TTF_RenderText_Blended_Wrapped(ttfont, string->data, textColor, 0);
|
||||
|
||||
text->bounds.w = surface->w;
|
||||
text->bounds.h = surface->h;
|
||||
|
||||
if(text->texture){
|
||||
SDL_DestroyTexture(text->texture);
|
||||
}
|
||||
text->texture = SDL_CreateTextureFromSurface((SDL_Renderer *)renderer, surface);
|
||||
|
||||
SDL_FreeSurface(surface);
|
||||
TTF_CloseFont(ttfont);
|
||||
}
|
||||
|
||||
void ARC_Text_Render(ARC_Text *text, ARC_Renderer *renderer){
|
||||
if(text->texture == NULL){
|
||||
return;
|
||||
}
|
||||
|
||||
SDL_Rect bounds = (SDL_Rect){ text->bounds.x, text->bounds.y, text->bounds.w, text->bounds.h };
|
||||
SDL_RenderCopy((SDL_Renderer *)renderer, text->texture, NULL, &bounds);
|
||||
}
|
||||
|
||||
void ARC_Text_SetPos(ARC_Text *text, ARC_Point pos){
|
||||
text->bounds.x = pos.x;
|
||||
text->bounds.y = pos.y;
|
||||
}
|
||||
20
packages/graphics/sdl/text.h
Normal file
20
packages/graphics/sdl/text.h
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
#ifndef ARC_SDL_TEXT_H_
|
||||
#define ARC_SDL_TEXT_H_
|
||||
|
||||
#include "arc/std/string.h"
|
||||
#include "arc/graphics/color.h"
|
||||
#include "arc/math/rectangle.h"
|
||||
|
||||
#include <SDL.h>
|
||||
|
||||
typedef struct ARC_Text {
|
||||
ARC_String *name;
|
||||
int32_t size;
|
||||
|
||||
ARC_Color color;
|
||||
|
||||
SDL_Texture *texture;
|
||||
ARC_Rect bounds;
|
||||
} ARC_Text;
|
||||
|
||||
#endif // !ARC_SDL_TEXT_H_
|
||||
35
packages/graphics/sdl/view.c
Normal file
35
packages/graphics/sdl/view.c
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
#include "arc/graphics/view.h"
|
||||
|
||||
#include "arc/std/errno.h"
|
||||
#include <SDL.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void ARC_View_Create(ARC_View **view, ARC_Renderer *renderer, ARC_Rect bounds){
|
||||
*view = (ARC_View *)malloc(sizeof(ARC_View));
|
||||
(*view)->renderer = renderer;
|
||||
(*view)->bounds = bounds;
|
||||
}
|
||||
|
||||
void ARC_View_Destroy(ARC_View *view){
|
||||
free(view);
|
||||
}
|
||||
|
||||
void ARC_View_Render(ARC_View *view, ARC_View_RenderFn renderFn, void *data){
|
||||
int err = SDL_RenderSetViewport((SDL_Renderer *)view->renderer, (const SDL_Rect *)&(view->bounds));
|
||||
if(err){
|
||||
ARC_DEBUG_LOG(ARC_ERRNO_DATA, "in src/graphics/sdl/view.c ARC_View_Render(view, renderFn), SDL_RenderSetViewport(...) returned: %d", err);
|
||||
return;
|
||||
}
|
||||
|
||||
renderFn(data);
|
||||
|
||||
err = SDL_RenderSetViewport((SDL_Renderer *)view->renderer, NULL);
|
||||
if(err){
|
||||
ARC_DEBUG_LOG(ARC_ERRNO_DATA, "in src/graphics/sdl/view.c ARC_View_Render(view, NULL), SDL_RenderSetViewport(...) returned: %d", err);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
ARC_Rect ARC_View_GetBounds(ARC_View *view){
|
||||
return view->bounds;
|
||||
}
|
||||
31
packages/graphics/sdl/window.c
Normal file
31
packages/graphics/sdl/window.c
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
#include "arc/graphics/window.h"
|
||||
|
||||
#include "window.h"
|
||||
#include "arc/std/errno.h"
|
||||
#include <SDL.h>
|
||||
|
||||
void ARC_Window_Create(ARC_Window **window, ARC_WindowInfo *info){
|
||||
if(!info){
|
||||
arc_errno = ARC_ERRNO_NULL;
|
||||
ARC_DEBUG_ERR("ARC_Window_Create(**window, NULL)");
|
||||
return;
|
||||
}
|
||||
|
||||
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0){
|
||||
arc_errno = ARC_ERRNO_INIT;
|
||||
printf("Error: initializing SDL\nSDL Error: %s\n", SDL_GetError());
|
||||
return;
|
||||
}
|
||||
|
||||
*window = (ARC_Window *)SDL_CreateWindow((const char *)info->title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, info->w, info->h, 0);
|
||||
|
||||
if(!*window){
|
||||
arc_errno = ARC_ERRNO_NULL;
|
||||
ARC_DEBUG_LOG(arc_errno, "SDL_CreateWindow(%s, %d, %d, %d, %d, %x);", info->title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, info->w, info->h, 0);
|
||||
free(window);
|
||||
}
|
||||
}
|
||||
|
||||
void ARC_Window_Destroy(ARC_Window *window){
|
||||
SDL_DestroyWindow((SDL_Window *) window);
|
||||
}
|
||||
9
packages/graphics/sdl/window.h
Normal file
9
packages/graphics/sdl/window.h
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
#ifndef ARC_SDL_WINDOW_H_
|
||||
#define ARC_SDL_WINDOW_H_
|
||||
|
||||
#include "arc/graphics/window.h"
|
||||
#include <SDL.h>
|
||||
|
||||
typedef SDL_Window ARC_WindowType;
|
||||
|
||||
#endif // !ARC_SDL_WINDOW_H_
|
||||
Loading…
Add table
Add a link
Reference in a new issue