merged herbglitch/master with local changes

This commit is contained in:
herbglitch 2024-01-19 01:19:17 -07:00
commit a0c02c0dcd
26 changed files with 329 additions and 38 deletions

7
src/audio/sdl/audio.c Normal file
View file

@ -0,0 +1,7 @@
#include "arc/audio/audio.h"
#include "arc/audio/sdl/audio.h"
#include <SDL2/SDL_mixer.h>
void ARC_Audio_Play(ARC_Audio *audio){
Mix_PlayChannel(-1, audio->chunk, 0);
}

43
src/audio/sdl/config.c Normal file
View file

@ -0,0 +1,43 @@
#include "arc/audio/config.h"
#include "arc/audio/sdl/audio.h"
#include <stdio.h>
#include <stdlib.h>
#include "arc/std/config.h"
#include "arc/std/errno.h"
#include "arc/audio/audio.h"
// #define ARC_DEFAULT_CONFIG
#include "arc/std/defaults/config.h"
void ARC_AudioConfig_Init(ARC_Config *config){
ARC_Config_AddKeyCString(config, (char *)"ARC_Audio", 9, ARC_Audio_Read, ARC_Audio_Delete);
}
uint8_t ARC_Audio_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_DEBUG_LOG(arc_errno, "in ARC_Point_Read(config, string, value); no matching quotes: %s", string->data);
arc_errno = ARC_ERRNO_DATA;
return 0;
}
ARC_Audio *audio = (ARC_Audio *)malloc(sizeof(ARC_Audio));
ARC_String *path;
ARC_String_CopySubstring(&path, string, 1, string->length - 2);
audio->chunk = Mix_LoadWAV(path->data);
//TODO: get error message if not loaded
*value = (void *)audio;
return 0;
}
void ARC_Audio_Delete(ARC_Config* config, ARC_String *string, void *value){
Mix_FreeChunk(((ARC_Audio *)value)->chunk);
free((ARC_Audio *)value);
}

View file

@ -20,12 +20,19 @@
#include "arc/input/sdl/mouse.h"
#include "arc/input/sdl/keyboard.h"
#include <SDL.h>
#include <SDL_video.h>
#include <SDL2/SDL_ttf.h>
<<<<<<< HEAD
#endif
#include "arc/graphics/glfw/window.h"
#include "arc/graphics/glfw/renderer.h"
=======
#include <SDL2/SDL_mixer.h>
#elif ARC_OPENGL
#include "arc/graphics/opengl/window.h"
#include "arc/graphics/opengl/renderer.h"
#ifdef ARC_GLFW
>>>>>>> af9a1f1040e60a663e175a3b36d752913941b973
#include "arc/input/glfw/mouse.h"
#include "arc/input/glfw/keyboard.h"
@ -46,6 +53,8 @@ void ARC_EngineData_Create(ARC_EngineData **data, ARC_Handler_CleanDataFn cleanf
//TEMP
#ifdef ARC_SDL
TTF_Init();
Mix_Init(0);
Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 2, 1024);
#endif
#ifdef ARC_SDL
@ -88,6 +97,8 @@ void ARC_EngineData_Create(ARC_EngineData **data, ARC_Handler_CleanDataFn cleanf
void ARC_EngineData_Destroy(ARC_EngineData *data){
#ifdef ARC_SDL
free(data->mouse->event);
TTF_Quit();
Mix_Quit();
#endif // ARC_SDL
ARC_Mouse_Destroy(data->mouse);
@ -103,7 +114,6 @@ void ARC_Engine_Run(ARC_EngineData *data){
}
#ifdef ARC_SDL
SDL_SetRenderDrawBlendMode((SDL_Renderer *)data->renderer, SDL_BLENDMODE_BLEND);
SDL_Event *event = data->mouse->event;
double lastTime = 0, currentTime;

View file

@ -40,4 +40,13 @@ void ARC_Circle_Render(ARC_Circle *circle, ARC_Renderer *renderer, ARC_Color *co
}
}
//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);
}
}
#endif // ARC_SDL

View file

@ -53,7 +53,10 @@ int32_t ARC_SDL_Texture_Load(const char *path, SDL_Texture **texture){
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();
@ -213,12 +216,7 @@ uint8_t ARC_Sprite_Read(ARC_Config* config, ARC_String *string, void **value){
}
}
//sprite
*value = malloc(sizeof(ARC_Sprite));
((ARC_Sprite *) *value)->frameIndex = malloc(sizeof(uint32_t));
((ARC_Sprite *) *value)->spritesheet = spritesheet;
((ARC_Sprite *) *value)->frames = frames;
*((ARC_Sprite *) *value)->frameIndex = 0;
ARC_Sprite_Create((ARC_Sprite **)value, spritesheet, frames);
return 0;
}

View file

@ -21,6 +21,8 @@ void ARC_Renderer_Create(ARC_Renderer **renderer, ARC_RenderInfo *info){
ARC_DEBUG_LOG(arc_errno, "SDL_CreateRenderer(%p, %d, %u);", info->window, info->index, info->flags);
free(renderer);
}
SDL_SetRenderDrawBlendMode((SDL_Renderer *)*renderer, SDL_BLENDMODE_BLEND);
}
void ARC_Renderer_Destroy(ARC_Renderer *renderer){

View file

@ -14,6 +14,7 @@ void ARC_Sprite_Create(ARC_Sprite **sprite, ARC_Spritesheet *spritesheet, ARC_Ar
(*sprite)->frames = frames;
(*sprite)->frameIndex = (uint32_t *)malloc(sizeof(uint32_t));
*(*sprite)->frameIndex = 0;
(*sprite)->opacity = 255;
}
void ARC_Sprite_Destroy(ARC_Sprite *sprite){
@ -28,7 +29,13 @@ void ARC_Sprite_Copy(ARC_Sprite **newSprite, ARC_Sprite *oldSprite){
*(*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);
}
@ -42,10 +49,14 @@ void ARC_Sprite_RenderFlip(ARC_Sprite *sprite, ARC_Renderer *renderer, ARC_Rect
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);
}
@ -66,6 +77,10 @@ void ARC_Sprite_IterateFrame(ARC_Sprite *sprite){
}
}
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;
}

35
src/graphics/sdl/view.c Normal file
View 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;
}

View file

@ -12,7 +12,7 @@ void ARC_Window_Create(ARC_Window **window, ARC_WindowInfo *info){
return;
}
if(SDL_Init(SDL_INIT_VIDEO) < 0){
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;

View file

@ -92,6 +92,7 @@ ARC_KeyboardState ARC_Keyboard_GetState(ARC_Keyboard *keyboard, enum ARC_Keyboar
case ARC_KEY_SPACE: return keyboard->keys[SDLK_SPACE ];
case ARC_KEY_ESC: return keyboard->keys[SDLK_ESCAPE];
case ARC_KEY_ENTER: return keyboard->keys[SDLK_RETURN];
default: return ARC_KEY_NONE;
}

View file

@ -85,7 +85,7 @@ uint8_t ARC_Rect_Read(ARC_Config *config, ARC_String *string, void **value){
}
ARC_String_CopySubstring(&temp, current, 0, separator - 1);
ARC_String_StripEndsWhitespace(temp, &tempStripped);
ARC_String_StripEndsWhitespace(&tempStripped, temp);
x = ARC_String_ToInt64_t(tempStripped);
ARC_String_Destroy(temp);
ARC_String_Destroy(tempStripped);
@ -101,7 +101,7 @@ uint8_t ARC_Rect_Read(ARC_Config *config, ARC_String *string, void **value){
}
ARC_String_CopySubstring(&temp, current, 0, separator - 1);
ARC_String_StripEndsWhitespace(temp, &tempStripped);
ARC_String_StripEndsWhitespace(&tempStripped, temp);
y = ARC_String_ToInt64_t(tempStripped);
ARC_String_Destroy(temp);
ARC_String_Destroy(tempStripped);
@ -117,7 +117,7 @@ uint8_t ARC_Rect_Read(ARC_Config *config, ARC_String *string, void **value){
}
ARC_String_CopySubstring(&temp, current, 0, separator - 1);
ARC_String_StripEndsWhitespace(temp, &tempStripped);
ARC_String_StripEndsWhitespace(&tempStripped, temp);
w = ARC_String_ToInt64_t(tempStripped);
ARC_String_Destroy(temp);
ARC_String_Destroy(tempStripped);
@ -133,7 +133,7 @@ uint8_t ARC_Rect_Read(ARC_Config *config, ARC_String *string, void **value){
}
ARC_String_CopySubstring(&temp, current, 0, separator);
ARC_String_StripEndsWhitespace(temp, &tempStripped);
ARC_String_StripEndsWhitespace(&tempStripped, temp);
h = ARC_String_ToInt64_t(tempStripped);
ARC_String_Destroy(temp);
ARC_String_Destroy(tempStripped);
@ -150,7 +150,7 @@ uint8_t ARC_Rect_Read(ARC_Config *config, ARC_String *string, void **value){
void ARC_RectArray_ReadRect(ARC_Config* config, ARC_String *stripped, uint64_t index, uint64_t length, uint64_t *arrayIndex, void **value){
ARC_String *substr, *temp;
ARC_String_CopySubstring(&temp, stripped, index, length);
ARC_String_StripEndsWhitespace(temp, &substr);
ARC_String_StripEndsWhitespace(&substr, temp);
ARC_String_Destroy(temp);
// reading in reference
@ -194,7 +194,7 @@ uint8_t ARC_RectArray_Read(ARC_Config* config, ARC_String *string, void **value)
ARC_String *temp, *stripped;
ARC_String_CopySubstring(&temp, string, 1, string->length - 2);
ARC_String_StripEndsWhitespace(temp, &stripped);
ARC_String_StripEndsWhitespace(&stripped, temp);
ARC_String_Destroy(temp);
uint64_t arraySize = 1;

8
src/math/point.c Normal file
View file

@ -0,0 +1,8 @@
#include "arc/math/point.h"
ARC_FPoint ARC_FPoint_Lerp(ARC_FPoint *start, ARC_FPoint *end, float t){
return (ARC_FPoint){
(1.0f - t) * start->x + t * end->x,
(1.0f - t) * start->y + t * end->y
};
}

View file

@ -195,7 +195,7 @@ void ARC_Config_SetKeyGroup(ARC_Config *config, ARC_String **data, uint8_t *comm
ARC_String *name, *temp;
ARC_String_CopySubstring(&temp, *data, index, nextIndex - index - 1);
ARC_String_StripEndsWhitespace(temp, &name);
ARC_String_StripEndsWhitespace(&name, temp);
ARC_String_Destroy(temp);
temp = *data;
@ -258,11 +258,11 @@ void ARC_Config_GetNameAndValue(ARC_String *data, ARC_String **name, ARC_String
index++;
ARC_String *dataTemp = *name;
ARC_String_StripEndsWhitespace(dataTemp, name);
ARC_String_StripEndsWhitespace(name, dataTemp);
ARC_String_Destroy(dataTemp);
ARC_String_CopySubstring(&dataTemp, data, index, data->length - index);
ARC_String_StripEndsWhitespace(dataTemp, value);
ARC_String_StripEndsWhitespace(value, dataTemp);
ARC_String_Destroy(dataTemp);
}
@ -276,7 +276,7 @@ void ARC_Config_Recurse(ARC_Config *config, ARC_String **data, ARC_String *group
while(*data && (*data)->length){
ARC_String *dataTemp = *data;
ARC_String_StripEndsWhitespace(dataTemp, data);
ARC_String_StripEndsWhitespace(data, dataTemp);
ARC_String_Destroy(dataTemp);
// break out of current group
@ -302,7 +302,7 @@ void ARC_Config_Recurse(ARC_Config *config, ARC_String **data, ARC_String *group
ARC_String *keyType, *keyTypeTemp;
ARC_String_CopySubstring(&keyTypeTemp, *data, 0, index);
ARC_String_StripEndsWhitespace(keyTypeTemp, &keyType);
ARC_String_StripEndsWhitespace(&keyType, keyTypeTemp);
ARC_String_Destroy(keyTypeTemp);
if(ARC_String_EqualsCString(keyType, "group", 5)){
@ -468,7 +468,7 @@ void ARC_Config_RunCommand(ARC_Config *config, ARC_String *command){
ARC_String *commandArgTemp, *commandArg;
ARC_String_CopySubstring(&commandArgTemp, command, index + space->length, command->length - (index + space->length));
ARC_String_StripWhitespace(commandArgTemp, &commandArg);
ARC_String_StripWhitespace(&commandArg, commandArgTemp);
ARC_String_Destroy(commandArgTemp);
if(ARC_String_EqualsCString(command, "load", 4)){
@ -548,7 +548,7 @@ void ARC_Config_FileIO(ARC_Config *config, ARC_String *path, uint8_t command){
ARC_String_Destroy(temp);
temp = data;
ARC_String_StripEndsWhitespace(temp, &data);
ARC_String_StripEndsWhitespace(&data, temp);
ARC_String_Destroy(temp);
ARC_Config_Recurse(config, &data, NULL, &command);

View file

@ -196,7 +196,7 @@ uint8_t ARC_ConfigKey_Read_String(ARC_Config* config, ARC_String *string, void *
void ARC_ConfigKey_StringArray_ReadString(ARC_Config* config, ARC_String *stripped, uint64_t index, uint64_t length, uint64_t *arrayIndex, void **value){
ARC_String *substr, *temp;
ARC_String_CopySubstring(&temp, stripped, index, length);
ARC_String_StripEndsWhitespace(temp, &substr);
ARC_String_StripEndsWhitespace(&substr, temp);
ARC_String_Destroy(temp);
// reading in reference
@ -239,7 +239,7 @@ uint8_t ARC_ConfigKey_Read_StringArray(ARC_Config* config, ARC_String *string, v
ARC_String *temp, *stripped;
ARC_String_CopySubstring(&temp, string, 1, string->length - 2);
ARC_String_StripEndsWhitespace(temp, &stripped);
ARC_String_StripEndsWhitespace(&stripped, temp);
ARC_String_Destroy(temp);
uint64_t arraySize = 1;

View file

@ -100,6 +100,18 @@ uint8_t ARC_String_EqualsCString(ARC_String *string, const char *cstring, uint64
return 1;
}
uint8_t ARC_String_SubstringEqualsCString(ARC_String *string, uint64_t offset, const char *cstring, uint64_t length){
if(string->length - offset < length){
return 0;
}
if(strncmp(string->data + offset, cstring, length)){
return 0;
}
return 1;
}
uint8_t ARC_String_Alpha(ARC_String *string){
for(uint64_t length = string->length; length; length--){
if(string->data[length - 1] >= 'a' && string->data[length - 1] <= 'z'){
@ -131,6 +143,7 @@ double ARC_String_ToDouble(ARC_String *string){
uint64_t ARC_String_Find(ARC_String *string, ARC_String *substring){
if(!string || !substring){
ARC_DEBUG_ERR("ARC_String_Find(string, substring), string or substring was null");
arc_errno = ARC_ERRNO_NULL;
return ~(uint64_t)0;
}
@ -152,6 +165,7 @@ uint64_t ARC_String_Find(ARC_String *string, ARC_String *substring){
uint64_t ARC_String_FindCString(ARC_String *string, const char *cstring, uint64_t length){
if(!string || !cstring){
arc_errno = ARC_ERRNO_NULL;
ARC_DEBUG_ERR("ARC_String_FindCString(string, cstring, length), string or cstring was null");
return ~(uint64_t)0;
}
@ -172,6 +186,7 @@ uint64_t ARC_String_FindCString(ARC_String *string, const char *cstring, uint64_
uint64_t ARC_String_FindBack(ARC_String *string, ARC_String *substring){
if(!string || !substring){
arc_errno = ARC_ERRNO_NULL;
ARC_DEBUG_ERR("ARC_String_FindBack(string, substring), string or substring was null");
return ~(uint64_t)0;
}
@ -189,7 +204,7 @@ uint64_t ARC_String_FindBack(ARC_String *string, ARC_String *substring){
return ~(uint64_t)0;
}
void ARC_String_StripEnds(ARC_String *original, ARC_String **stripped, char charToStrip){
void ARC_String_StripEnds(ARC_String **stripped, ARC_String *original, char charToStrip){
if(!original){
arc_errno = ARC_ERRNO_NULL;
*stripped = NULL;
@ -232,7 +247,7 @@ void ARC_String_StripEnds(ARC_String *original, ARC_String **stripped, char char
ARC_String_Create(stripped, original->data + start, length);
}
void ARC_String_StripWhitespace(ARC_String *original, ARC_String **stripped){
void ARC_String_StripWhitespace(ARC_String **stripped, ARC_String *original){
if(!original){
arc_errno = ARC_ERRNO_NULL;
*stripped = NULL;
@ -303,7 +318,7 @@ void ARC_String_StripWhitespace(ARC_String *original, ARC_String **stripped){
ARC_String_Create(stripped, data, length);
}
void ARC_String_StripEndsWhitespace(ARC_String *original, ARC_String **stripped){
void ARC_String_StripEndsWhitespace(ARC_String **stripped, ARC_String *original){
uint64_t index;
for(uint64_t i = 0; i < original->length; i++){
if(original->data[i] == ' '){
@ -351,7 +366,7 @@ void ARC_String_StripEndsWhitespace(ARC_String *original, ARC_String **stripped)
ARC_String_CopySubstring(stripped, original, index, endIndex - index);
}
void ARC_String_Merge(ARC_String *first, ARC_String *second, ARC_String **combined){
void ARC_String_Merge(ARC_String **combined, ARC_String *first, ARC_String *second){
char data[first->length + second->length];
for(uint32_t i = 0; i < first->length; i++){
data[i] = first->data[i];
@ -385,7 +400,7 @@ void ARC_String_RemoveSection(ARC_String **newString, ARC_String *original, uint
ARC_String_CopySubstring(&first , original, 0 , removeIndex );
ARC_String_CopySubstring(&second, original, removeIndex + removeLength, original->length - (removeIndex + removeLength));
ARC_String_Merge(first, second, newString);
ARC_String_Merge(newString, first, second);
ARC_String_Destroy(first );
ARC_String_Destroy(second);