f***ed up and needed to rework packages

This commit is contained in:
herbglitch 2024-05-20 03:46:09 -06:00
parent b43ab1702f
commit f7a87d7519
78 changed files with 3713 additions and 0 deletions

View file

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

View file

@ -0,0 +1,10 @@
#ifndef ARC_SDL_AUDIO_H_
#define ARC_SDL_AUDIO_H_
#include <SDL2/SDL_mixer.h>
typedef struct ARC_Audio {
Mix_Chunk *chunk;
} ARC_Audio;
#endif // !ARC_SDL_AUDIO_H_

View file

@ -0,0 +1,43 @@
#include "arc/audio/config.h"
#include "audio.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

@ -0,0 +1,147 @@
#include "arc/console/buffer/line.h"
#include "arc/console/view.h"
#include "arc/std/string.h"
#include "arc/std/vector.h"
#include <stdlib.h>
#include <string.h>
struct ARC_ConsoleLineBuffer {
ARC_Vector *bufferLines;
};
void ARC_ConsoleLineBuffer_Create(ARC_ConsoleLineBuffer **buffer){
*buffer = (ARC_ConsoleLineBuffer *)malloc(sizeof(ARC_ConsoleLineBuffer));
ARC_Vector_Create(&((*buffer)->bufferLines));
//add first line to vector
ARC_Vector_Add((*buffer)->bufferLines, NULL);
}
void ARC_ConsoleLineBuffer_Destroy(ARC_ConsoleLineBuffer *buffer){
for(uint32_t i = 0; i < ARC_Vector_Size(buffer->bufferLines); i++){
ARC_String *bufferLine = (ARC_String *)ARC_Vector_Get(buffer->bufferLines, i);
if(bufferLine != NULL){
ARC_String_Destroy(bufferLine);
}
}
ARC_Vector_Destroy(buffer->bufferLines);
free(buffer);
}
void ARC_ConsoleLineBuffer_Clear(ARC_ConsoleLineBuffer *buffer){
for(uint32_t i = 0; i < ARC_Vector_Size(buffer->bufferLines); i++){
ARC_String *bufferLine = (ARC_String *)ARC_Vector_Get(buffer->bufferLines, i);
if(bufferLine != NULL){
ARC_String_Destroy(bufferLine);
}
}
ARC_Vector_Destroy(buffer->bufferLines);
ARC_Vector_Create(&(buffer->bufferLines));
//add first line to vector
ARC_Vector_Add(buffer->bufferLines, NULL);
}
void ARC_ConsoleLineBuffer_Render(ARC_ConsoleLineBuffer *buffer, ARC_ConsoleView *view){
ARC_Rect viewBounds = ARC_ConsoleView_GetBounds(view);
uint32_t bufferStartIndex = 0;
if(ARC_Vector_Size(buffer->bufferLines) > (uint32_t)viewBounds.h){
bufferStartIndex = ARC_Vector_Size(buffer->bufferLines) - viewBounds.h;
}
for(uint32_t i = 0; i < (uint32_t)viewBounds.h; i++){
if(i + bufferStartIndex > ARC_Vector_Size(buffer->bufferLines)){
break;
}
ARC_String *bufferLine = (ARC_String *)ARC_Vector_Get(buffer->bufferLines, i + bufferStartIndex);
if(bufferLine == NULL){
continue;
}
ARC_ConsoleView_RenderStringAt(view, bufferLine, (ARC_Point){ 0, i });
}
}
void ARC_ConsoleLineBuffer_RenderSection(ARC_ConsoleLineBuffer *buffer, ARC_ConsoleView *view, uint32_t startIndex, uint32_t lines){
ARC_Rect viewBounds = ARC_ConsoleView_GetBounds(view);
for(uint32_t i = 0; i < lines; i++){
if(i + startIndex >= ARC_Vector_Size(buffer->bufferLines)){
break;
}
if(i >= (uint32_t)viewBounds.h){
break;
}
ARC_String *bufferLine = (ARC_String *)ARC_Vector_Get(buffer->bufferLines, i + startIndex);
if(bufferLine == NULL){
continue;
}
ARC_ConsoleView_RenderStringAt(view, bufferLine, (ARC_Point){ 0, i });
}
}
void ARC_ConsoleLineBuffer_AddChar(ARC_ConsoleLineBuffer *buffer, char character){
if(character == '\n'){
ARC_Vector_Add(buffer->bufferLines, (void *)NULL);
return;
}
//get the last line and add a char to it
ARC_String *bufferLine = (ARC_String *)ARC_Vector_Get(buffer->bufferLines, ARC_Vector_Size(buffer->bufferLines) - 1);
ARC_Vector_RemoveIndex(buffer->bufferLines, ARC_Vector_Size(buffer->bufferLines) - 1);
if(bufferLine == NULL){
ARC_String_Create(&bufferLine, &character, 1);
ARC_Vector_Add(buffer->bufferLines, (void *)bufferLine);
return;
}
//add char to the end of the bufferline
ARC_String *nextChar;
ARC_String_Create(&nextChar, &character, 1);
ARC_String *tempBufferLine = bufferLine;
ARC_String_Merge(&bufferLine, tempBufferLine, nextChar);
ARC_String_Destroy(tempBufferLine);
ARC_String_Destroy(nextChar);
//add buffer line back to the bufferLines
ARC_Vector_Add(buffer->bufferLines, (void *)bufferLine);
}
void ARC_ConsoleLineBuffer_AddString(ARC_ConsoleLineBuffer *buffer, ARC_String *string){
//TODO: this in a more efficient way
for(uint64_t i = 0; i < string->length; i++){
ARC_ConsoleLineBuffer_AddChar(buffer, string->data[i]);
}
}
void ARC_ConsoleLineBuffer_AddCString(ARC_ConsoleLineBuffer *buffer, char *cstring, uint64_t length){
//TODO: this in a more efficient way
for(uint64_t i = 0; i < length; i++){
ARC_ConsoleLineBuffer_AddChar(buffer, cstring[i]);
}
}
void ARC_ConsoleLineBuffer_AddCStringWithStrlen(ARC_ConsoleLineBuffer *buffer, char *cstring){
//TODO: this in a more efficient way
for(uint64_t i = 0; i < strlen(cstring); i++){
ARC_ConsoleLineBuffer_AddChar(buffer, cstring[i]);
}
}
uint32_t ARC_ConsoleLineBuffer_GetLineNumbers(ARC_ConsoleLineBuffer *buffer){
return ARC_Vector_Size(buffer->bufferLines);
}

View file

@ -0,0 +1,51 @@
#include "arc/console/buffer/view.h"
#include "arc/console/view.h"
#include "arc/math/point.h"
#include <stdlib.h>
#include <stdint.h>
struct ARC_ConsoleViewBuffer {
ARC_Point bounds;
char **buffer;
};
void ARC_ConsoleViewBuffer_Create(ARC_ConsoleViewBuffer **buffer, ARC_ConsoleView *view){
*buffer = (ARC_ConsoleViewBuffer *)malloc(sizeof(ARC_ConsoleViewBuffer));
ARC_Rect viewBounds = ARC_ConsoleView_GetBounds(view);
(*buffer)->bounds = (ARC_Point){ viewBounds.w - viewBounds.x, viewBounds.h - viewBounds.y };
//create the buffer array
(*buffer)->buffer = (char **)malloc(sizeof(char *) * (*buffer)->bounds.y);
for(int32_t y = 0; y < (*buffer)->bounds.y ; y++){
(*buffer)->buffer[y] = (char *)malloc(sizeof(char) * (*buffer)->bounds.x);
}
ARC_ConsoleViewBuffer_Clear(*buffer);
}
void ARC_ConsoleViewBuffer_Destroy(ARC_ConsoleViewBuffer *buffer){
for(int32_t y = 0; y < buffer->bounds.y; y++){
free(buffer->buffer[y]);
}
free(buffer->buffer);
free(buffer);
}
void ARC_ConsoleViewBuffer_Clear(ARC_ConsoleViewBuffer *buffer){
for(int32_t y = 0; y < buffer->bounds.y ; y++){
for(int32_t x = 0; x < buffer->bounds.x; x++){
buffer->buffer[y][x] = ' ';
}
}
}
void ARC_ConsoleViewBuffer_Render(ARC_ConsoleViewBuffer *buffer, ARC_ConsoleView *view){
for(int32_t y = 0; y < buffer->bounds.y ; y++){
for(int32_t x = 0; x < buffer->bounds.x; x++){
ARC_ConsoleView_RenderCharAt(view, buffer->buffer[y][x], (ARC_Point){ x, y });
}
}
}

View file

@ -0,0 +1,52 @@
#include "arc/console/element.h"
#include "arc/console/view.h"
#include <stdlib.h>
#include <ncurses.h>
void ARC_ConsoleElement_Create(ARC_ConsoleElement **element, uint32_t type, uint8_t flags, ARC_String *string, ARC_Point pos, ARC_ConsoleElement_RenderFn renderFn){
*element = (ARC_ConsoleElement *)malloc(sizeof(ARC_ConsoleElement));
(*element)->type = type;
(*element)->flags = flags;
(*element)->string = string;
(*element)->pos = pos;
(*element)->renderFn = renderFn;
}
void ARC_ConsoleElement_Destroy(ARC_ConsoleElement *element){
free(element);
}
void ARC_ConsoleElement_DefaultRenderFn(ARC_ConsoleView *view, ARC_ConsoleElement *element){
if(element->flags & ARC_CONSOLE_ELEMENT_FLAG_SELECTED){
ARC_ConsoleView_SetAttribute(view, ARC_CONSOLE_VIEW_ATTRIBUTE_REVERSE);
}
ARC_ConsoleView_RenderStringAt(view, element->string, element->pos);
if(element->flags & ARC_CONSOLE_ELEMENT_FLAG_SELECTED){
ARC_ConsoleView_SetAttribute(view, ARC_CONSOLE_VIEW_ATTRIBUTE_NONE);
}
}
ARC_Bool ARC_ConsoleElement_IsSelectable(ARC_ConsoleElement *element){
if(element->flags & ARC_CONSOLE_ELEMENT_FLAG_SELECTABLE){
return ARC_True;
}
return ARC_False;
}
void ARC_ConsoleElement_SetSelected(ARC_ConsoleElement *element, ARC_Bool selected){
if(selected){
element->flags |= ARC_CONSOLE_ELEMENT_FLAG_SELECTED;
return;
}
element->flags &= ~ARC_CONSOLE_ELEMENT_FLAG_SELECTED;
}
void ARC_NCursesElement_ToggleSelected(ARC_ConsoleElement *element){
element->flags ^= ARC_CONSOLE_ELEMENT_FLAG_SELECTED;
}

View file

@ -0,0 +1,161 @@
#include "arc/console/key.h"
#include "key.h"
#include "arc/std/bool.h"
#include <ncurses.h>
#include <stdlib.h>
void ARC_ConsoleKey_Create(ARC_ConsoleKey **consoleKey, ARC_ConsoleKey_Key *key){
*consoleKey = (ARC_ConsoleKey *)malloc(sizeof(ARC_ConsoleKey));
(*consoleKey)->key = 0;
if(key != NULL){
(*consoleKey)->key = *key;
}
}
void ARC_ConsoleKey_Destroy(ARC_ConsoleKey *consoleKey){
free(consoleKey);
}
ARC_Bool ARC_ConsoleKey_Equals(ARC_ConsoleKey consoleKey, ARC_ConsoleKey_Key key){
return consoleKey.key == ARC_Keyboard_GetConsoleKey(key).key;
}
ARC_Bool ARC_ConsoleKey_EqualsPointer(ARC_ConsoleKey *consoleKey, ARC_ConsoleKey_Key key){
return consoleKey->key == ARC_Keyboard_GetConsoleKey(key).key;
}
ARC_ConsoleKey ARC_Keyboard_GetConsoleKey(enum ARC_ConsoleKey_Key key){
switch(key){
case ARC_KEY_A:
return (ARC_ConsoleKey){ (int32_t)'a' };
case ARC_KEY_B:
return (ARC_ConsoleKey){ (int32_t)'b' };
case ARC_KEY_C:
return (ARC_ConsoleKey){ (int32_t)'c' };
case ARC_KEY_D:
return (ARC_ConsoleKey){ (int32_t)'d' };
case ARC_KEY_E:
return (ARC_ConsoleKey){ (int32_t)'e' };
case ARC_KEY_F:
return (ARC_ConsoleKey){ (int32_t)'f' };
case ARC_KEY_G:
return (ARC_ConsoleKey){ (int32_t)'g' };
case ARC_KEY_H:
return (ARC_ConsoleKey){ (int32_t)'h' };
case ARC_KEY_I:
return (ARC_ConsoleKey){ (int32_t)'i' };
case ARC_KEY_J:
return (ARC_ConsoleKey){ (int32_t)'j' };
case ARC_KEY_K:
return (ARC_ConsoleKey){ (int32_t)'k' };
case ARC_KEY_L:
return (ARC_ConsoleKey){ (int32_t)'l' };
case ARC_KEY_M:
return (ARC_ConsoleKey){ (int32_t)'m' };
case ARC_KEY_N:
return (ARC_ConsoleKey){ (int32_t)'n' };
case ARC_KEY_O:
return (ARC_ConsoleKey){ (int32_t)'o' };
case ARC_KEY_P:
return (ARC_ConsoleKey){ (int32_t)'p' };
case ARC_KEY_Q:
return (ARC_ConsoleKey){ (int32_t)'q' };
case ARC_KEY_R:
return (ARC_ConsoleKey){ (int32_t)'r' };
case ARC_KEY_S:
return (ARC_ConsoleKey){ (int32_t)'s' };
case ARC_KEY_T:
return (ARC_ConsoleKey){ (int32_t)'t' };
case ARC_KEY_U:
return (ARC_ConsoleKey){ (int32_t)'u' };
case ARC_KEY_V:
return (ARC_ConsoleKey){ (int32_t)'v' };
case ARC_KEY_W:
return (ARC_ConsoleKey){ (int32_t)'w' };
case ARC_KEY_X:
return (ARC_ConsoleKey){ (int32_t)'x' };
case ARC_KEY_Y:
return (ARC_ConsoleKey){ (int32_t)'y' };
case ARC_KEY_Z:
return (ARC_ConsoleKey){ (int32_t)'z' };
case ARC_KEY_0:
return (ARC_ConsoleKey){ (int32_t)'0' };
case ARC_KEY_1:
return (ARC_ConsoleKey){ (int32_t)'1' };
case ARC_KEY_2:
return (ARC_ConsoleKey){ (int32_t)'2' };
case ARC_KEY_3:
return (ARC_ConsoleKey){ (int32_t)'3' };
case ARC_KEY_4:
return (ARC_ConsoleKey){ (int32_t)'4' };
case ARC_KEY_5:
return (ARC_ConsoleKey){ (int32_t)'5' };
case ARC_KEY_6:
return (ARC_ConsoleKey){ (int32_t)'6' };
case ARC_KEY_7:
return (ARC_ConsoleKey){ (int32_t)'7' };
case ARC_KEY_8:
return (ARC_ConsoleKey){ (int32_t)'8' };
case ARC_KEY_9:
return (ARC_ConsoleKey){ (int32_t)'9' };
case ARC_KEY_UP:
return (ARC_ConsoleKey){ KEY_UP };
case ARC_KEY_DOWN:
return (ARC_ConsoleKey){ KEY_DOWN };
case ARC_KEY_LEFT:
return (ARC_ConsoleKey){ KEY_LEFT };
case ARC_KEY_RIGHT:
return (ARC_ConsoleKey){ KEY_RIGHT };
//TODO: This is escape and alt, need to fix
case ARC_KEY_ESC:
return (ARC_ConsoleKey){ 27 };
default:
return (ARC_ConsoleKey){ 0 };
}
}
uint8_t ARC_ConsoleKey_GetCharFromKey(ARC_ConsoleKey *consoleKey){
return consoleKey->key;
}

View file

@ -0,0 +1,19 @@
#ifndef ARC_CONSOLE_NCURSES_KEY_H_
#define ARC_CONSOLE_NCURSES_KEY_H_
#ifdef __cplusplus
extern "C" {
#endif
#include "arc/console/key.h"
#include <stdint.h>
typedef struct ARC_ConsoleKeyType {
int32_t key;
} ARC_ConsoleKeyType;
#ifdef __cplusplus
}
#endif
#endif // !ARC_CONSOLE_NCURSES_KEY_H_

View file

@ -0,0 +1,57 @@
#include "arc/console/shell.h"
#include "arc/console/buffer.h"
#include "arc/std/string.h"
#include "arc/std/vector.h"
#include <stdlib.h>
void ARC_ConsoleShell_Create(ARC_ConsoleShell **shell, ARC_ConsoleView *view, ARC_ConsoleShell_UpdateFn updateFn){
*shell = (ARC_ConsoleShell *)malloc(sizeof(ARC_ConsoleShell));
(*shell)->view = view;
ARC_ConsoleBuffer_Create(&((*shell)->buffer));
(*shell)->bufferLineIndex = 0;
ARC_Vector_Create(&((*shell)->history));
(*shell)->historyIndex = 0;
(*shell)->updateFn = updateFn;
(*shell)->currentLine = NULL;
(*shell)->userInput = NULL;
}
void ARC_ConsoleShell_Destroy(ARC_ConsoleShell *shell){
ARC_ConsoleBuffer_Destroy(shell->buffer);
for(uint32_t i = 0; i < ARC_Vector_Size(shell->history); i++){
ARC_String *temp = (ARC_String *)ARC_Vector_Get(shell->history, i);
if(temp != NULL){
ARC_String_Destroy(temp);
}
}
ARC_Vector_Destroy(shell->history);
free(shell);
}
void ARC_ConsoleShell_Update(ARC_ConsoleShell *shell){
shell->updateFn(shell);
}
void ARC_ConsoleShell_Render(ARC_ConsoleShell *shell){
ARC_ConsoleBuffer_Render(shell->buffer, shell->view);
}
void ARC_ConsoleShell_AddHistory(ARC_ConsoleShell *shell, ARC_String *string){
ARC_Vector_Add(shell->history, (void *)string);
}
ARC_String *ARC_ConsoleShell_GetHistoryAt(ARC_ConsoleShell *shell, uint32_t index){
uint32_t maxHistory = ARC_Vector_Size(shell->history);
if(index >= maxHistory){
return NULL;
}
return (ARC_String *)ARC_Vector_Get(shell->history, (maxHistory - 1) - index);
}

View file

@ -0,0 +1,268 @@
#include "arc/console/view.h"
#include "key.h"
#include "arc/console/element.h"
#include "arc/std/bool.h"
#include "arc/std/errno.h"
#include "arc/std/vector.h"
#include "arc/std/string.h"
#include <locale.h>
#include <ncurses.h>
#include <stdlib.h>
uint8_t arc_ncurses_win_size = 0;
struct ARC_ConsoleView {
WINDOW *window;
ARC_Rect bounds;
ARC_Bool echo;
ARC_Vector *elements;
};
void ARC_ConsoleView_Create(ARC_ConsoleView **view, ARC_Rect bounds){
if(arc_ncurses_win_size == ~(uint8_t)0){
arc_errno = ARC_ERRNO_OVERFLOW;
ARC_DEBUG_ERR("ARC_NCurses_Create(ncurses), max num of ARC_NCurses have been created, consider making arc_ncurses_win_size a uint32_t to increase the max");
*view = NULL;
return;
}
//if this is the first ncurses, init ncurses
if(arc_ncurses_win_size == 0){
setlocale(LC_ALL, "");
initscr();
//start_color();
cbreak();
keypad(stdscr, TRUE);
refresh();
}
*view = (ARC_ConsoleView *)malloc(sizeof(ARC_ConsoleView));
ARC_Rect viewBounds = { 0, 0, COLS, LINES };
if(bounds.w != 0 && bounds.h != 0){
viewBounds = bounds;
}
(*view)->window = newwin(viewBounds.h, viewBounds.w, viewBounds.y, viewBounds.x);
(*view)->bounds = viewBounds;
keypad((*view)->window, TRUE);
noecho();
(*view)->echo = false;
ARC_Vector_Create(&(*view)->elements);
wrefresh((*view)->window);
arc_ncurses_win_size++;
}
void ARC_ConsoleView_Destroy(ARC_ConsoleView *ncurses){
arc_ncurses_win_size--;
ARC_Vector_Destroy(ncurses->elements);
delwin(ncurses->window);
free(ncurses);
if(arc_ncurses_win_size == 0){
endwin();
}
}
void ARC_ConsoleView_AddElement(ARC_ConsoleView *view, ARC_ConsoleElement *element){
ARC_Vector_Add(view->elements, (void *)element);
}
void ARC_ConsoleView_RemoveElement(ARC_ConsoleView *view, uint32_t index){
ARC_Vector_RemoveIndex(view->elements, index);
}
void ARC_ConsoleView_Clear(ARC_ConsoleView *view){
wclear(view->window);
wrefresh(view->window);
}
void ARC_ConsoleView_RenderCharAt(ARC_ConsoleView *view, char character, ARC_Point pos){
mvwprintw(view->window, pos.y, pos.x, "%c", character);
wrefresh(view->window);
}
void ARC_ConsoleView_RenderWCharAt(ARC_ConsoleView *view, wchar_t character, ARC_Point pos){
mvwprintw(view->window, pos.y, pos.x, "%lc", character);
wrefresh(view->window);
}
void ARC_ConsoleView_RenderKeyAt(ARC_ConsoleView *view, ARC_ConsoleKey key, ARC_Point pos){
mvwprintw(view->window, pos.y, pos.x, "%c", (char)key.key);
wrefresh(view->window);
}
void ARC_ConsoleView_RenderUint32At(ARC_ConsoleView *view, uint32_t uint32, ARC_Point pos){
mvwprintw(view->window, pos.y, pos.x, "%d", uint32);
wrefresh(view->window);
}
void ARC_ConsoleView_RenderStringAt(ARC_ConsoleView *view, ARC_String *text, ARC_Point pos){
mvwprintw(view->window, pos.y, pos.x, "%s", text->data);
wrefresh(view->window);
}
void ARC_ConsoleView_RenderCStringWithStrlenAt(ARC_ConsoleView *view, char *cstr, ARC_Point pos){
mvwprintw(view->window, pos.y, pos.x, "%s", cstr);
wrefresh(view->window);
}
void ARC_ConsoleView_RenderRect(ARC_ConsoleView *view, ARC_Rect bounds){
//render corners
ARC_ConsoleView_RenderWCharAt(view, L'', (ARC_Point){ bounds.x, bounds.y });
ARC_ConsoleView_RenderWCharAt(view, L'', (ARC_Point){ bounds.x, (bounds.h - 1) + bounds.y });
ARC_ConsoleView_RenderWCharAt(view, L'', (ARC_Point){ (bounds.w - 1) + bounds.x, bounds.y });
ARC_ConsoleView_RenderWCharAt(view, L'', (ARC_Point){ (bounds.w - 1) + bounds.x, (bounds.h - 1) + bounds.y });
//render virticle lines
for(int32_t x = 1; x < bounds.w - 1; x++){
ARC_ConsoleView_RenderWCharAt(view, L'', (ARC_Point){ bounds.x + x, bounds.y });
ARC_ConsoleView_RenderWCharAt(view, L'', (ARC_Point){ bounds.x + x, (bounds.h - 1) + bounds.y });
}
//render horizontal lines
for(int32_t y = 1; y < bounds.h - 1; y++){
ARC_ConsoleView_RenderWCharAt(view, L'', (ARC_Point){ bounds.x, bounds.y + y });
ARC_ConsoleView_RenderWCharAt(view, L'', (ARC_Point){ (bounds.w - 1) + bounds.x, bounds.y + y });
}
}
void ARC_ConsoleView_RenderElements(ARC_ConsoleView *view){
for(uint32_t i = 0; i < ARC_Vector_Size(view->elements); i++){
ARC_ConsoleElement *element = (ARC_ConsoleElement *)ARC_Vector_Get(view->elements, i);
element->renderFn(view, element);
wrefresh(view->window);
}
}
ARC_Rect ARC_ConsoleView_GetBounds(ARC_ConsoleView *view){
return view->bounds;
}
ARC_ConsoleElement *ARC_ConsoleView_GetElement(ARC_ConsoleView *view, uint32_t index){
return (ARC_ConsoleElement *)ARC_Vector_Get(view->elements, index);
}
char ARC_ConsoleView_GetChar(ARC_ConsoleView *view){
return wgetch(view->window);
}
char ARC_ConsoleView_GetCharAt(ARC_ConsoleView *view, ARC_Point pos){
return mvwgetch(view->window, pos.y, pos.x);
}
ARC_ConsoleKey ARC_ConsoleView_GetConsoleKeyAt(ARC_ConsoleView *view, ARC_Point pos){
return (ARC_ConsoleKey){ mvwgetch(view->window, pos.y, pos.x) };
}
ARC_ConsoleKey *ARC_ConsoleView_GetCreateConsoleKeyAt(ARC_ConsoleView *view, ARC_Point pos){
ARC_ConsoleKey *key;
ARC_ConsoleKey_Create(&key, NULL);
key->key = mvwgetch(view->window, pos.y, pos.x);
return key;
}
ARC_String *ARC_ConsoleView_GetStringInput(ARC_ConsoleView *view, ARC_Point pos, ARC_ConsoleView_OverrideCharInputFn *overrideCharInputFn, void *userdata){
noecho();
uint32_t cstringSize = view->bounds.w - pos.x;
char cstring[view->bounds.w - pos.x];
ARC_ConsoleKey temp = ARC_ConsoleView_GetConsoleKeyAt(view, pos);
uint32_t index = 0;
while(temp.key != '\n'){
//store the last size to be able to clear efficeintly
uint32_t lastSize = index;
//if override function exists and it overrode the current char
if(overrideCharInputFn != NULL && (*overrideCharInputFn)(&temp, cstring, &index, cstringSize, userdata)){
for(uint32_t i = 0; i < lastSize; i++){
ARC_ConsoleView_RenderCharAt(view, ' ', (ARC_Point){ pos.x + i, pos.y });
}
for(uint32_t i = 0; i < index; i++){
ARC_ConsoleView_RenderCharAt(view, cstring[i], (ARC_Point){ pos.x + i, pos.y });
}
temp = ARC_ConsoleView_GetConsoleKeyAt(view, (ARC_Point){ pos.x + index, pos.y });
continue;
}
if(temp.key == KEY_BACKSPACE || temp.key == KEY_DC || temp.key == 127){
if(index == 0){
temp = ARC_ConsoleView_GetConsoleKeyAt(view, (ARC_Point){ pos.x + index, pos.y });
continue;
}
index--;
ARC_ConsoleView_RenderCharAt(view, ' ', (ARC_Point){ pos.x + index, pos.y });
cstring[index] = '\0';
temp = ARC_ConsoleView_GetConsoleKeyAt(view, (ARC_Point){ pos.x + index, pos.y });
continue;
}
if(index < (view->bounds.w - 1) - pos.x){
ARC_ConsoleView_RenderCharAt(view, (char)(temp.key), (ARC_Point){ pos.x + index, pos.y });
cstring[index] = (char)(temp.key);
index++;
}
temp = ARC_ConsoleView_GetConsoleKeyAt(view, (ARC_Point){ pos.x + index, pos.y });
}
if(view->echo){
echo();
}
if(index == 0){
return NULL;
}
ARC_String *string;
ARC_String_Create(&string, cstring, index);
return string;
}
void ARC_ConsoleView_SetCursorVisibility(ARC_ConsoleView *view, uint8_t visibility){
switch(visibility){
case ARC_CONSOLE_VIEW_CURSOR_HIDDEN:
curs_set(0);
break;
case ARC_CONSOLE_VIEW_CURSOR_VISIBLE:
curs_set(1);
break;
}
}
void ARC_ConsoleView_SetBorder(ARC_ConsoleView *view, uint32_t border){
switch(border){
case ARC_CONSOLE_VIEW_BORDER_NONE:
wborder(view->window, ' ', ' ', ' ',' ',' ',' ',' ',' ');
break;
case ARC_CONSOLE_VIEW_BORDER_DEFAULT:
box(view->window, 0, 0);
break;
}
wrefresh(view->window);
}
void ARC_ConsoleView_SetAttribute(ARC_ConsoleView *view, uint32_t attribute){
switch(attribute){
case ARC_CONSOLE_VIEW_ATTRIBUTE_NONE:
wattroff(view->window, A_REVERSE);
break;
case ARC_CONSOLE_VIEW_ATTRIBUTE_REVERSE:
wattron(view->window, A_REVERSE);
break;
}
}

View 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

View 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

View 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_

View 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

View 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_

View 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

View 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

View 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

View 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

View 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

View 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_

View 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

View 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_

View 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

View 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_

View 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);
}
}

View 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);
}

View 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);
}

View 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);
}

View 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);
}

View 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);
}

View 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_

View 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;
}

View 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_

View 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;
}

View 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_

View 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;
}

View 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_

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

@ -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);
}

View 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_

View file

@ -0,0 +1,42 @@
#ifdef ARC_GLFW_INPUT
#include "arc/input/input.h"
#include "arc/input/mouse.h"
#include "arc/input/keyboard.h"
#include <GLFW/glfw3.h>
#include <stdlib.h>
struct ARC_Input {
ARC_Keyboard *keyboard;
ARC_Mouse *mouse;
GLFWwindow *window;
};
void ARC_Input_CreateWithEngineData(ARC_Input **input, ARC_EngineData *data){
*input = (ARC_Input *)malloc(sizeof(ARC_Input));
(*input)->event = (SDL_Event *)malloc(sizeof(SDL_Event));
ARC_Keyboard_CreateWithEngineData(&(*input)->keyboard, data);
ARC_Keyboard_CreateWithEngineData(&(*input)->mouse, data);
}
void ARC_Input_Destroy(ARC_Input *input){
ARC_Keyboard_Destroy(input->keyboard);
ARC_Mouse_Destroy(input->mouse);
free(input->event);
free(input);
}
ARC_Keyboard *ARC_Input_GetKeyboard(ARC_Input *input){
return input->keyboard;
}
ARC_Mouse *ARC_Input_GetMouse(ARC_Input *input){
return input->mouse;
}
#endif // ARC_SDL2_INPUT

View file

@ -0,0 +1,23 @@
#ifdef ARC_GLFW_INPUT
#include "arc/input/keyboard.h"
#include "keyboard.h"
#include "arc/math/point.h"
#include "arc/std/errno.h"
#include <stdlib.h>
#include <stdint.h>
void ARC_Keyboard_CreateWithInput(ARC_Keyboard **keyboard, ARC_Input *input){
}
void ARC_Keyboard_Destroy(ARC_Keyboard *keyboard){
}
void ARC_Keyboard_Update(ARC_Keyboard *keyboard){
}
ARC_KeyboardState ARC_Keyboard_GetState(ARC_Keyboard *keyboard, enum ARC_KeyboardKey key){
return ARC_KEY_NONE;
}
#endif // ARC_GLFW_INPUT

View file

@ -0,0 +1,21 @@
#ifndef ARC_GLFW_KEYBOARD_H_
#define ARC_GLFW_KEYBOARD_H_
#ifdef ARC_GLFW_INPUT
#include <GLFW/glfw3.h>
#include "arc/input/keyboard.h"
struct ARC_Keyboard {
GLFWwindow *window;
ARC_KeyboardState *keys;
ARC_KeyboardState *released;
};
struct ARC_KeyboardInfo {
GLFWwindow *window;
};
#endif // !ARC_GLFW_INPUT
#endif // !ARC_GLFW_KEYBOARD_H_

View file

@ -0,0 +1,34 @@
#ifdef ARC_GLFW_INPUT
#include "arc/input/mouse.h"
#include "mouse.h"
#include "arc/math/point.h"
#include "arc/std/errno.h"
#include <stdlib.h>
#include <stdint.h>
void ARC_Mouse_CreateWithInput(ARC_Keyboard **keyboard, ARC_Input *input){
}
void ARC_Mouse_Destroy(ARC_Mouse *mouse){
}
void ARC_Mouse_UpdateButton(ARC_Mouse *mouse, uint8_t button, uint32_t *buttons, uint32_t mask){
}
void ARC_Mouse_Update(ARC_Mouse *mouse){
}
ARC_Point *ARC_Mouse_GetCoords(ARC_Mouse *mouse){
return mouse->coords;
}
ARC_MouseState ARC_Mouse_GetState(ARC_Mouse *mouse, ARC_MouseButton button){
return mouse->buttons[button];
}
int32_t *ARC_Mouse_GetScrollY(ARC_Mouse *mouse){
return mouse->scrollY;
}
#endif // ARC_SDL_INPUT

View file

@ -0,0 +1,25 @@
#ifndef ARC_GLFW_MOUSE_H_
#define ARC_GLFW_MOUSE_H_
#ifdef ARC_GLFW_INPUT
#include <GLFW/glfw3.h>
#include "arc/input/mouse.h"
#include "arc/math/point.h"
struct ARC_Mouse {
GLFWwindow *window;
ARC_Point *coords;
int32_t *scrollY;
ARC_MouseState *buttons;
uint8_t *buttonsReleased;
};
struct ARC_MouseInfo {
GLFWwindow *window;
};
#endif // !ARC_GLFW_INPUT
#endif // !ARC_GLFW_MOUSE_H_

View file

@ -0,0 +1,40 @@
#include "arc/input/input.h"
#include "input.h"
#include "arc/input/mouse.h"
#include "arc/input/keyboard.h"
#include <SDL.h>
#include <stdlib.h>
void ARC_Input_CreateWithEngineData(ARC_Input **input, ARC_EngineData *data){
*input = (ARC_Input *)malloc(sizeof(ARC_Input));
(*input)->event = (SDL_Event *)malloc(sizeof(SDL_Event));
ARC_Keyboard_CreateWithInput(&((*input)->keyboard), *input);
ARC_Mouse_CreateWithInput(&((*input)->mouse), *input);
}
void ARC_Input_Destroy(ARC_Input *input){
ARC_Keyboard_Destroy(input->keyboard);
ARC_Mouse_Destroy(input->mouse);
free(input->event);
free(input);
}
void ARC_Input_Update(ARC_Input *input){
SDL_PollEvent(input->event);
ARC_Keyboard_Update(input->keyboard);
ARC_Mouse_Update(input->mouse);
}
ARC_Keyboard *ARC_Input_GetKeyboard(ARC_Input *input){
return input->keyboard;
}
ARC_Mouse *ARC_Input_GetMouse(ARC_Input *input){
return input->mouse;
}

View file

@ -0,0 +1,15 @@
#ifndef ARC_SDL_INPUT_H_
#define ARC_SDL_INPUT_H_
#include "arc/input/keyboard.h"
#include "arc/input/mouse.h"
#include <SDL.h>
struct ARC_Input {
ARC_Keyboard *keyboard;
ARC_Mouse *mouse;
SDL_Event *event;
};
#endif // !ARC_SDL_INPUT_H_

View file

@ -0,0 +1,98 @@
#include "arc/input/keyboard.h"
#include "keyboard.h"
#include "input.h"
#include "arc/math/point.h"
#include "arc/std/errno.h"
#include <stdlib.h>
#include <stdint.h>
#include <SDL_keyboard.h>
#include <SDL_events.h>
void ARC_Keyboard_CreateWithInput(ARC_Keyboard **keyboard, ARC_Input *input){
*keyboard = (ARC_Keyboard *)malloc(sizeof(ARC_Keyboard));
(*keyboard)->event = input->event;
(*keyboard)->keys = (ARC_KeyboardState *)malloc(sizeof(ARC_KeyboardState) * ARC_KEYBOARD_BUTTON_NUM);
(*keyboard)->released = NULL;
for(uint8_t i = 0; i < ARC_KEYBOARD_BUTTON_NUM; i++){
(*keyboard)->keys[i] = ARC_KEY_NONE;
}
}
void ARC_Keyboard_Destroy(ARC_Keyboard *keyboard){
free(keyboard->keys);
free(keyboard);
}
void ARC_Keyboard_Update(ARC_Keyboard *keyboard){
if(keyboard->released){
*keyboard->released = ARC_KEY_NONE;
keyboard->released = NULL;
}
if(keyboard->event->type != SDL_KEYDOWN && keyboard->event->type != SDL_KEYUP){
return;
}
if(keyboard->event->key.keysym.sym >= 239 || keyboard->event->key.keysym.sym < 0){
return;
}
if(keyboard->event->type == SDL_KEYDOWN){
keyboard->keys[keyboard->event->key.keysym.sym] = ARC_KEY_PRESSED;
return;
}
keyboard->keys[keyboard->event->key.keysym.sym] = ARC_KEY_RELEASED;
keyboard->released = (keyboard->keys + keyboard->event->key.keysym.sym);
}
ARC_KeyboardState ARC_Keyboard_GetState(ARC_Keyboard *keyboard, enum ARC_KeyboardKey key){
switch(key){
case ARC_KEY_A: return keyboard->keys[SDLK_a];
case ARC_KEY_B: return keyboard->keys[SDLK_b];
case ARC_KEY_C: return keyboard->keys[SDLK_c];
case ARC_KEY_D: return keyboard->keys[SDLK_d];
case ARC_KEY_E: return keyboard->keys[SDLK_e];
case ARC_KEY_F: return keyboard->keys[SDLK_f];
case ARC_KEY_G: return keyboard->keys[SDLK_g];
case ARC_KEY_H: return keyboard->keys[SDLK_h];
case ARC_KEY_I: return keyboard->keys[SDLK_i];
case ARC_KEY_J: return keyboard->keys[SDLK_j];
case ARC_KEY_K: return keyboard->keys[SDLK_k];
case ARC_KEY_L: return keyboard->keys[SDLK_l];
case ARC_KEY_M: return keyboard->keys[SDLK_m];
case ARC_KEY_N: return keyboard->keys[SDLK_n];
case ARC_KEY_O: return keyboard->keys[SDLK_o];
case ARC_KEY_P: return keyboard->keys[SDLK_p];
case ARC_KEY_Q: return keyboard->keys[SDLK_q];
case ARC_KEY_R: return keyboard->keys[SDLK_r];
case ARC_KEY_S: return keyboard->keys[SDLK_s];
case ARC_KEY_T: return keyboard->keys[SDLK_t];
case ARC_KEY_U: return keyboard->keys[SDLK_u];
case ARC_KEY_V: return keyboard->keys[SDLK_v];
case ARC_KEY_W: return keyboard->keys[SDLK_w];
case ARC_KEY_X: return keyboard->keys[SDLK_x];
case ARC_KEY_Y: return keyboard->keys[SDLK_y];
case ARC_KEY_Z: return keyboard->keys[SDLK_z];
case ARC_KEY_0: return keyboard->keys[SDLK_0];
case ARC_KEY_1: return keyboard->keys[SDLK_1];
case ARC_KEY_2: return keyboard->keys[SDLK_2];
case ARC_KEY_3: return keyboard->keys[SDLK_3];
case ARC_KEY_4: return keyboard->keys[SDLK_4];
case ARC_KEY_5: return keyboard->keys[SDLK_5];
case ARC_KEY_6: return keyboard->keys[SDLK_6];
case ARC_KEY_7: return keyboard->keys[SDLK_7];
case ARC_KEY_8: return keyboard->keys[SDLK_8];
case ARC_KEY_9: return keyboard->keys[SDLK_9];
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

@ -0,0 +1,14 @@
#ifndef ARC_SDL_KEYBOARD_H_
#define ARC_SDL_KEYBOARD_H_
#include "arc/input/keyboard.h"
#include <SDL.h>
struct ARC_Keyboard {
SDL_Event *event;
ARC_KeyboardState *keys;
ARC_KeyboardState *released;
};
#endif // !ARC_SDL_KEYBOARD_H_

105
packages/input/sdl/mouse.c Normal file
View file

@ -0,0 +1,105 @@
#include "arc/input/mouse.h"
#include "mouse.h"
#include "input.h"
#include "arc/math/point.h"
#include "arc/std/errno.h"
#include <stdlib.h>
#include <stdint.h>
#include <SDL_mouse.h>
#include <SDL_events.h>
void ARC_Mouse_CreateWithInput(ARC_Mouse **mouse, ARC_Input *input){
*mouse = (ARC_Mouse *)malloc(sizeof(ARC_Mouse));
(*mouse)->event = input->event;
(*mouse)->coords = (ARC_Point *)malloc(sizeof(ARC_Point));
(*mouse)->scrollY = (int32_t *)malloc(sizeof(int32_t ));
(*mouse)->buttons = (ARC_MouseState *)malloc(sizeof(ARC_MouseState) * ARC_MOUSE_BUTTON_NUM);
(*mouse)->buttonsReleased = (uint8_t *)malloc(sizeof(uint8_t));
*(*mouse)->coords = (ARC_Point){0, 0};
*(*mouse)->scrollY = 0;
for(uint8_t i = 0; i < ARC_MOUSE_BUTTON_NUM; i++){
(*mouse)->buttons[i] = ARC_MOUSE_NONE;
}
*(*mouse)->buttonsReleased = 0;
}
void ARC_Mouse_Destroy(ARC_Mouse *mouse){
free(mouse->buttonsReleased);
free(mouse->buttons);
free(mouse->scrollY);
free(mouse->coords );
free(mouse);
}
void ARC_Mouse_UpdateButton(ARC_Mouse *mouse, uint8_t button, uint32_t *buttons, uint32_t mask){
if(*buttons & mask){
mouse->buttons[button] = ARC_MOUSE_PRESSED;
return;
}
if(mouse->buttons[button] == ARC_MOUSE_NONE){
return;
}
if(mouse->buttons[button] == ARC_MOUSE_RELEASED){
mouse->buttons[button] = ARC_MOUSE_NONE;
--*mouse->buttonsReleased;
return;
}
mouse->buttons[button] = ARC_MOUSE_RELEASED;
++*mouse->buttonsReleased;
}
void ARC_Mouse_Update(ARC_Mouse *mouse){
*mouse->scrollY = 0;
if(mouse->event->type == SDL_MOUSEWHEEL){
*mouse->scrollY = mouse->event->wheel.y;
}
uint32_t buttons = SDL_GetMouseState(&(mouse->coords->x), &(mouse->coords->y));
if(mouse->event->type != SDL_MOUSEBUTTONDOWN && mouse->event->type != SDL_MOUSEBUTTONUP){
if(!*mouse->buttonsReleased){
return;
}
for(uint8_t i = *mouse->buttonsReleased; i > 0; i--){
if(mouse->buttons[i - 1] == ARC_MOUSE_RELEASED){
mouse->buttons[i - 1] = ARC_MOUSE_NONE;
--*mouse->buttonsReleased;
}
}
// if(*mouse->buttonsReleased){
// arc_errno = ARC_ERRNO_DATA;
// ARC_DEBUG_LOG(arc_errno, "in ARC_Mouse_Update mouse->buttonsReleased == %u, it needs to be 0\n", *(mouse->buttonsReleased));
// }
return;
}
ARC_Mouse_UpdateButton(mouse, ARC_MOUSE_LEFT , &buttons, SDL_BUTTON_LMASK );
ARC_Mouse_UpdateButton(mouse, ARC_MOUSE_MIDDLE, &buttons, SDL_BUTTON_MMASK );
ARC_Mouse_UpdateButton(mouse, ARC_MOUSE_RIGHT , &buttons, SDL_BUTTON_RMASK );
ARC_Mouse_UpdateButton(mouse, ARC_MOUSE_X1 , &buttons, SDL_BUTTON_X1MASK);
ARC_Mouse_UpdateButton(mouse, ARC_MOUSE_X2 , &buttons, SDL_BUTTON_X2MASK);
}
ARC_Point *ARC_Mouse_GetCoords(ARC_Mouse *mouse){
return mouse->coords;
}
ARC_MouseState ARC_Mouse_GetState(ARC_Mouse *mouse, ARC_MouseButton button){
return mouse->buttons[button];
}
int32_t *ARC_Mouse_GetScrollY(ARC_Mouse *mouse){
return mouse->scrollY;
}

View file

@ -0,0 +1,17 @@
#ifndef ARC_SDL_MOUSE_H_
#define ARC_SDL_MOUSE_H_
#include <SDL.h>
#include "arc/input/mouse.h"
#include "arc/math/point.h"
struct ARC_Mouse {
SDL_Event *event;
ARC_Point *coords;
int32_t *scrollY;
ARC_MouseState *buttons;
uint8_t *buttonsReleased;
};
#endif // !ARC_SDL_MOUSE_H_

View file

@ -0,0 +1,207 @@
#include "arc/networking/ssh.h"
#include "arc/std/errno.h"
#include <stdint.h>
#include <stdlib.h>
#include <libssh/libssh.h>
#include <errno.h>
#include <string.h>
struct ARC_Ssh {
ssh_session session;
};
int verify_knownhost(ssh_session session){
enum ssh_known_hosts_e state;
unsigned char *hash = NULL;
ssh_key srv_pubkey = NULL;
size_t hlen;
char buf[10];
char *hexa;
char *p;
int cmp;
int rc;
rc = ssh_get_server_publickey(session, &srv_pubkey);
if(rc < 0){
return -1;
}
rc = ssh_get_publickey_hash(srv_pubkey, SSH_PUBLICKEY_HASH_SHA1, &hash, &hlen);
ssh_key_free(srv_pubkey);
if(rc < 0){
return -1;
}
state = ssh_session_is_known_server(session);
switch(state){
case SSH_KNOWN_HOSTS_OK:
/* OK */
break;
case SSH_KNOWN_HOSTS_CHANGED:
fprintf(stderr, "Host key for server changed: it is now:\n");
//ssh_print_hexa("Public key hash", hash, hlen);
fprintf(stderr, "For security reasons, connection will be stopped\n");
ssh_clean_pubkey_hash(&hash);
return -1;
case SSH_KNOWN_HOSTS_OTHER:
fprintf(stderr, "The host key for this server was not found but an other type of key exists.\n");
fprintf(stderr, "An attacker might change the default server key to confuse your client into thinking the key does not exist\n");
ssh_clean_pubkey_hash(&hash);
return -1;
case SSH_KNOWN_HOSTS_NOT_FOUND:
fprintf(stderr, "Could not find known host file.\n");
fprintf(stderr, "If you accept the host key here, the file will be automatically created.\n");
/* FALL THROUGH to SSH_SERVER_NOT_KNOWN behavior */
case SSH_KNOWN_HOSTS_UNKNOWN:
hexa = ssh_get_hexa(hash, hlen);
fprintf(stderr,"The server is unknown. Do you trust the host key?\n");
fprintf(stderr, "Public key hash: %s\n", hexa);
ssh_string_free_char(hexa);
ssh_clean_pubkey_hash(&hash);
p = fgets(buf, sizeof(buf), stdin);
if(p == NULL){
return -1;
}
cmp = strncasecmp(buf, "yes", 3);
if(cmp != 0){
return -1;
}
rc = ssh_session_update_known_hosts(session);
if(rc < 0){
fprintf(stderr, "Error %s\n", strerror(errno));
return -1;
}
break;
case SSH_KNOWN_HOSTS_ERROR:
fprintf(stderr, "Error %s", ssh_get_error(session));
ssh_clean_pubkey_hash(&hash);
return -1;
}
ssh_clean_pubkey_hash(&hash);
return 0;
}
void ARC_Ssh_Create(ARC_Ssh **ssh, char *host, char *user, char *password){
*ssh = (ARC_Ssh *)malloc(sizeof(ARC_Ssh));
(*ssh)->session = ssh_new();
if((*ssh)->session == NULL){
arc_errno = ARC_ERRNO_NULL;
ARC_DEBUG_ERR("ARC_Ssh_Create(ssh), ssh session could not be created\n");
free(*ssh);
*ssh = NULL;
return;
}
if(host != NULL){
ssh_options_set((*ssh)->session, SSH_OPTIONS_HOST, host);
}
if(user != NULL){
ssh_options_set((*ssh)->session, SSH_OPTIONS_USER, user);
}
int32_t returnCode = ssh_connect((*ssh)->session);
if(returnCode != SSH_OK){
arc_errno = ARC_ERRNO_CONNECTION;
ARC_DEBUG_LOG(arc_errno, "ARC_Ssh_Create(ssh), ssh failed to connect to localhost: %s\n", ssh_get_error((*ssh)->session));
ssh_free((*ssh)->session);
free(*ssh);
*ssh = NULL;
return;
}
if(verify_knownhost((*ssh)->session) < 0){
arc_errno = ARC_ERRNO_CONNECTION;
ARC_DEBUG_ERR("ARC_Ssh_Create(ssh), ssh failed to verify knownhost\n");
ssh_disconnect((*ssh)->session);
ssh_free((*ssh)->session);
free(*ssh);
*ssh = NULL;
}
returnCode = ssh_userauth_password((*ssh)->session, NULL, password);
if(returnCode != SSH_AUTH_SUCCESS){
arc_errno = ARC_ERRNO_CONNECTION;
ARC_DEBUG_LOG(arc_errno, "ARC_Ssh_Create(ssh), ssh failed to authenticate with password: %s\n", ssh_get_error((*ssh)->session));
ssh_disconnect((*ssh)->session);
ssh_free((*ssh)->session);
free(*ssh);
*ssh = NULL;
}
}
void ARC_Ssh_Destroy(ARC_Ssh *ssh){
if(ssh == NULL){
return;
}
ssh_disconnect(ssh->session);
ssh_free(ssh->session);
free(ssh);
}
void ARC_Ssh_RunInSession(ARC_Ssh *ssh, ARC_Ssh_SessionFn sessionFn){
ssh_channel channel;
channel = ssh_channel_new(ssh->session);
if(channel == NULL){
arc_errno = ARC_ERRNO_NULL;
ARC_DEBUG_ERR("ARC_Ssh_RunInSession(ssh, sessionFn), ssh failed to create channel\n");
return;
}
int32_t returnCode = ssh_channel_open_session(channel);
if(returnCode != SSH_OK){
ssh_channel_free(channel);
arc_errno = ARC_ERRNO_CONNECTION;
ARC_DEBUG_LOG(arc_errno, "ARC_Ssh_RunInSession(ssh, sessionFn), ssh failed to open session with return code: %d\n", returnCode);
return;
}
// sessionFn(ssh, );
returnCode = ssh_channel_request_exec(channel, "export DISPLAY=:0 ; volume --inc");
printf("return code: %d\n", returnCode);
ssh_channel_send_eof(channel);
ssh_channel_close(channel);
ssh_channel_free(channel);
}
void ARC_Ssh_ExecStrInNewSession(ARC_Ssh *ssh, char *command){
ssh_channel channel;
channel = ssh_channel_new(ssh->session);
if(channel == NULL){
arc_errno = ARC_ERRNO_NULL;
ARC_DEBUG_ERR("ARC_Ssh_RunInSession(ssh, sessionFn), ssh failed to create channel\n");
return;
}
int32_t returnCode = ssh_channel_open_session(channel);
if(returnCode != SSH_OK){
ssh_channel_free(channel);
arc_errno = ARC_ERRNO_CONNECTION;
ARC_DEBUG_LOG(arc_errno, "ARC_Ssh_RunInSession(ssh, sessionFn), ssh failed to open session with return code: %d\n", returnCode);
return;
}
returnCode = ssh_channel_request_exec(channel, command);
if(returnCode != SSH_OK){
arc_errno = ARC_ERRNO_DATA;
ARC_DEBUG_LOG(arc_errno, "ARC_Ssh_RunInSession(ssh, sessionFn), ssh failed when executing command with error code: %d\n", returnCode);
}
ssh_channel_send_eof(channel);
ssh_channel_close(channel);
ssh_channel_free(channel);
}