Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************************************************/
- /******************************************************************************/
- //"2024-11-21\src\kit_sdl2\kit_EventWatch.cpp":
- #include "_kit_common.hpp"
- namespace kit {
- //found in "kit_func_convertEvent.cpp"
- bool _convertEvent(SDL_Event& e_sdl, Event& e_kit);
- static int _EventWatchCallbackWrapper(void* EventWatch_ptr, SDL_Event* e_sdl){
- //ignore all poll sentinel events
- if(e_sdl->type == SDL_POLLSENTINEL) return 0;
- //if _disabled is nonzero, exit early
- if(KIT_GET_CLASS_DATA(EventWatch_ptr)&0xff) return 0;
- EventWatchCallback callback = (EventWatchCallback)KIT_GET_CLASS_OPAQUE(EventWatch_ptr);
- void* userdata = KIT_GET_CLASS_OPAQUE2(EventWatch_ptr);
- Event e_kit;
- //only invoke callback if event conversion was successful
- if(_convertEvent(*e_sdl, e_kit) && callback != nullptr){
- try {
- callback(e_kit, userdata);
- } catch(const char* errortext){
- kit_LogError("IN EVENTWATCH CALLBACK: \"%s\"", errortext);
- freeThreadErrors();
- }
- }
- return 0;
- }
- EventWatch::EventWatch(EventWatchCallback callback, void* userdata,
- EventWatchDestructorCallback onDestruction)
- {
- if(_valid) return;
- _type = KIT_CLASSTYPE_EVENTWATCH;
- if(callback == nullptr)
- THROW_ERROR("EventWatch::EventWatch(): callback = nullptr");
- _callback = callback;
- _userdata = userdata;
- _onDestruction = onDestruction;
- SDL_AddEventWatch(_EventWatchCallbackWrapper, this);
- _valid = true;
- _constructing = false;
- }
- EventWatch::~EventWatch(){
- if(!_valid) return;
- _valid = false;
- SDL_DelEventWatch(_EventWatchCallbackWrapper, this);
- if(_onDestruction != nullptr)
- _onDestruction(_userdata);
- }
- }; /* namespace kit */
- /******************************************************************************/
- /******************************************************************************/
- //"2024-11-21\src\kit_sdl2\kit_fileio.cpp":
- #include "_kit_common.hpp"
- #if defined(_WIN32)
- #include <windows.h>
- #else
- int remove(const char *); //only used by fileio::remove
- //a wrapper; fileio::remove and remove are treated as the same inside fileio
- static inline int _remove(const char* filePath){ return remove(filePath); }
- #endif /* _WIN32 */
- namespace kit {
- BinaryData::BinaryData(const char* filePath){
- if(_valid) return;
- _type = KIT_CLASSTYPE_BINARYDATA;
- _data = (char*)SDL_LoadFile(filePath, &_data_size);
- if(_data == nullptr)
- THROW_ERRORF("BinaryData::BinaryData(file): \"%s\"", SDL_GetError());
- //numAllocations is explicitly incremented here, since the other constructor
- //calls memory::alloc, which does that automatically
- //both SDL_LoadFile and memory::alloc use SDL_malloc to allocate memory,
- //numAllocations will be decremented in the destructor properly either way
- ++numAllocations;
- _valid = true;
- _constructing = false;
- }
- BinaryData::BinaryData(const void* data, size_t data_size){
- if(_valid) return;
- _type = KIT_CLASSTYPE_BINARYDATA;
- if(data_size == KIT_U64_MAX)
- THROW_ERROR("BinaryData::BinaryData(memory): data_size = KIT_U64_MAX"); //lol
- _data = (char*)memory::alloc(data_size+1);
- if(_data == nullptr)
- THROW_ERROR("BinaryData::BinaryData(memory): failed to allocate memory for _data");
- _data_size = data_size;
- _data[data_size] = 0; //add null-terminator for convenience
- if(data != nullptr) memory::copy(_data, data, data_size);
- _valid = true;
- _constructing = false;
- }
- BinaryData::~BinaryData(){
- if(!_valid) return;
- _valid = false;
- if(_data != nullptr) memory::free(&_data);
- }
- //unlike the other fileio functions and whatnot,
- //this obviously doesn't throw if the file doesn't exist
- bool fileio::exists(const char* filePath){
- if(filePath == nullptr)
- THROW_ERROR("fileio::exists(): filePath = nullptr");
- SDL_RWops* file = SDL_RWFromFile(filePath, "rb");
- if(file != nullptr)
- SDL_RWclose(file);
- return file != nullptr;
- }
- size_t fileio::size(const char* filePath){
- if(filePath == nullptr)
- THROW_ERROR("fileio::size(): filePath = nullptr");
- SDL_RWops* file = SDL_RWFromFile(filePath, "rb");
- if(file == nullptr)
- THROW_ERRORF("fileio::size(): \"%s\"", SDL_GetError());
- if(SDL_RWseek(file, 0, RW_SEEK_END)<0){
- SDL_RWclose(file);
- THROW_ERRORF("fileio::size(): \"%s\"", SDL_GetError());
- }
- s64 fileSize = SDL_RWtell(file);
- if(fileSize < 0){
- //since SDL_RWclose() can overwrite the previous result of SDL_GetError(),
- //the result of fstr is cached before SDL_RWclose() is actually called
- const char* fstr_result = fstr("fileio::size(): \"%s\"", SDL_GetError());
- SDL_RWclose(file);
- THROW_ERROR(fstr_result);
- }
- SDL_RWclose(file);
- return (size_t)fileSize;
- }
- void fileio::remove(const char* filePath){
- if(filePath == nullptr)
- THROW_ERROR("fileio::remove(): filePath = nullptr");
- #if defined(_WIN32)
- if(DeleteFileA(filePath) == FALSE)
- #else
- if(_remove(filePath) != 0) //this is a wrapper for remove() (see line 9)
- #endif /* _WIN32 */
- THROW_ERRORF("fileio::remove(): failed to delete file \"%s\"", filePath);
- }
- //heap memory is allocated here,
- //and should be freed with memory::free()
- //(also, while *dataSize_p won't be populated with resulting
- // filesize if dataSize_p is nullptr, it won't actually error)
- void* fileio::readAll(const char* filePath, size_t* dataSize_p){
- if(filePath == nullptr)
- THROW_ERROR("fileio::readAll(): filePath = nullptr");
- void* data = SDL_LoadFile(filePath, dataSize_p);
- if(data == nullptr)
- THROW_ERRORF("fileio::readAll(): \"%s\"", SDL_GetError());
- ++numAllocations; //memory::free'ing data should undo this
- return data;
- }
- //writes to a binary file from a buffer
- void fileio::writeAll(const char* filePath, const void* data,
- size_t dataSize, bool append)
- {
- if(filePath == nullptr) THROW_ERROR("fileio::writeAll(): filePath = nullptr");
- if(data == nullptr) THROW_ERROR("fileio::writeAll(): data = nullptr");
- SDL_RWops* file = SDL_RWFromFile(filePath, (append) ? "ab" : "wb");
- if(file == nullptr){
- _throw_sdlerr:
- THROW_ERRORF("fileio::writeAll(): \"%s\"", SDL_GetError());
- }
- size_t bytesWritten = SDL_RWwrite(file, data, 1, dataSize);
- SDL_RWclose(file);
- if(bytesWritten < dataSize){
- SDL_SetError("bytesWritten < dataSize");
- goto _throw_sdlerr;
- }
- }
- }; /* namespace kit */
- /******************************************************************************/
- /******************************************************************************/
- //"2024-11-21\src\kit_sdl2\kit_fileio_File.cpp":
- #include "_kit_common.hpp"
- #define KIT_IS_INVALID_FILE (!_valid)
- #define KIT_FILE_CHECK(_funcName) \
- if(KIT_IS_INVALID_FILE){ THROW_ERROR(_funcName ": invalid File"); } \
- if(_file == nullptr ){ THROW_ERROR(_funcName ": _file = nullptr"); } \
- if(_closed ){ THROW_ERROR(_funcName ": file is already closed"); }
- #define FILE_PTR ((SDL_RWops*)_file)
- #define SIZE() size(FILE_PTR)
- #define READ(_elems, _elemSize, _elems_len) read(FILE_PTR, _elems, _elemSize, _elems_len)
- #define WRITE(_elems, _elemSize, _elems_len) write(FILE_PTR, _elems, _elemSize, _elems_len)
- #define SEEK(_off, _whence) seek(FILE_PTR, _off, _whence)
- #define TELL() seek(FILE_PTR, 0, RW_SEEK_CUR)
- #define CLOSE() close(FILE_PTR)
- namespace kit {
- File::File(const char* filePath, const char* mode){
- if(_valid) return;
- _type = KIT_CLASSTYPE_FILE;
- _file = (GenOpqPtr)SDL_RWFromFile(filePath, mode);
- if(_file == nullptr) THROW_ERRORF("File::File(): \"%s\"", SDL_GetError());
- _valid = true;
- _constructing = false;
- }
- File::~File(){
- if(!_valid) return;
- _valid = false;
- if(_file != nullptr){
- _zombie = FILE_PTR->CLOSE()<0;
- _file = nullptr;
- }
- }
- size_t File::read(void* elements, size_t elementSize, size_t elements_len){
- KIT_FILE_CHECK("File::read()");
- size_t result = FILE_PTR->READ(elements, elementSize, elements_len);
- if(!result && !strnCmp("Error", SDL_GetError()))
- THROW_ERRORF("File::read(): \"%s\"", SDL_GetError());
- return result;
- }
- void File::write(const void* elements, size_t elementSize, size_t elements_len){
- KIT_FILE_CHECK("File::write()");
- size_t result = FILE_PTR->WRITE(elements, elementSize, elements_len);
- if(result < elements_len)
- THROW_ERRORF("File::write(): \"%s\"", SDL_GetError());
- }
- size_t File::seek(s64 offset, u32 whence){
- KIT_FILE_CHECK("File::seek()");
- if(whence > 2) THROW_ERROR("File::seek(): whence is invalid");
- s64 result = FILE_PTR->SEEK(offset, whence);
- if(result < 0) THROW_ERRORF("File::seek(): \"%s\"", SDL_GetError());
- return (size_t)result;
- }
- void File::close(){
- KIT_FILE_CHECK("File::close()");
- if(FILE_PTR->CLOSE() < 0)
- THROW_ERROR("File::close(): write error occurred while flushing data");
- _closed = true;
- _file = nullptr;
- }
- size_t File::size(){
- KIT_FILE_CHECK("File::size()");
- s64 result = FILE_PTR->SIZE();
- if(result < 0)
- THROW_ERROR("File::size(): failed to get file's size");
- return (size_t)result;
- }
- }; /* namespace kit */
- /******************************************************************************/
- /******************************************************************************/
- //"2024-11-21\src\kit_sdl2\kit_func.cpp":
- #include "_kit_common.hpp"
- #include "../stb_sprintf/stb_sprintf.hpp"
- namespace kit {
- void Log(LogPriorityEnum priority, const char* fmt, ...){
- //(unnecessary?)
- //if(priority == 0 || priority > LOG_PRIORITY_CRITICAL)
- // THROW_ERROR("Log(): unknown priority");
- va_list args;
- va_start(args, fmt);
- SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION, (SDL_LogPriority)priority, fmt, args);
- va_end(args);
- }
- //(len_max does not include null-terminator!)
- //(if !len_max, call is analogous to str(not n)len)
- size_t strnLen(const char* str, size_t len_max){
- //if(str == nullptr) THROW_ERROR("strnLen(): str = nullptr");
- size_t len = 0;
- if(!len_max){
- for(; str[len]; ++len);
- } else {
- for(; str[len] && len<len_max; ++len);
- }
- return len;
- }
- //(len_max does not include null-terminator!)
- //(if !len_max, call is analogous to str(not n)cmp)
- s32 strnCmp(const char* str_a, const char* str_b, size_t len_max){
- //might uncomment these, maybe not though
- //if(str_a == nullptr) THROW_ERROR("strnCmp(): str_a = nullptr");
- //if(str_b == nullptr) THROW_ERROR("strnCmp(): str_b = nullptr");
- if(!len_max){
- while(*str_a && (*str_a == *str_b))
- ++str_a, ++str_b;
- } else {
- --len_max;
- while(*str_a && (*str_a == *str_b) && len_max)
- ++str_a, ++str_b, --len_max;
- }
- return (*(const u8*)str_a) - (*(const u8*)str_b);
- }
- char* strnCpy(char* str_dst, const char* str_src, size_t len_max){
- char* _str_dst = str_dst; //copy original state of str_dst
- if(!len_max){
- while((*str_dst++ = *str_src++));
- } else {
- size_t i = 0;
- while(i++ != len_max && (*str_dst++ = *str_src++));
- }
- *str_dst = 0; //null-terminator
- return _str_dst;
- }
- s32 snPrintf(char* str_dst, size_t len_max, const char* str_fmt, ...){
- va_list va;
- va_start(va, str_fmt);
- if(len_max > 0 && len_max <= KIT_S32_MAX){
- return stbsp_vsnprintf(str_dst, (int)len_max, str_fmt, va);
- } else {
- return stbsp_vsprintf(str_dst, str_fmt, va);
- }
- va_end(va);
- }
- s32 vsnPrintf(char* str_dst, size_t len_max, const char* str_fmt, va_list va){
- if(len_max > 0 && len_max <= KIT_S32_MAX){
- return stbsp_vsnprintf(str_dst, (int)len_max, str_fmt, va);
- } else {
- return stbsp_vsprintf(str_dst, str_fmt, va);
- }
- }
- const char* getLastSysError(){
- return SDL_GetError();
- }
- void clearSysError(){
- SDL_ClearError();
- }
- void freeThreadErrors(){
- while(_freeError()); //frees errors of *current* thread
- }
- #define MSGBOX_EWI_FLAGS (MSGBOX_ERROR|MSGBOX_WARNING|MSGBOX_INFO)
- void showMsgBox(const char* text, const char* title, u32 flags, Window* win){
- flags &= MSGBOX_EWI_FLAGS;
- SDL_Window* win_sdl = nullptr;
- if(win != nullptr){
- if(KIT_GET_CLASS_TYPE(win) != KIT_CLASSTYPE_WINDOW)
- THROW_ERROR("showMsgBoxSimple(): win does not point to a Window object");
- win_sdl = (SDL_Window*)KIT_GET_CLASS_OPAQUE(win);
- }
- if(SDL_ShowSimpleMessageBox(flags, title, text, win_sdl)<0)
- THROW_ERRORF("showMsgBoxSimple(): \"%s\"", SDL_GetError());
- }
- bool clipboardHasText(){
- return SDL_HasClipboardText();
- }
- char* clipboardGetText(){
- char* txt = SDL_GetClipboardText();
- //txt will be an empty string on error
- if(!txt[0]){
- //according to the SDL2 wiki, you need to
- //free the returned pointer, even on error
- SDL_free(txt);
- THROW_ERRORF("clipboardGetText(): \"%s\"", SDL_GetError());
- }
- ++numAllocations; //a subsequent call to memory::free should undo this
- return txt;
- }
- void clipboardSetText(const char* txt){
- if(SDL_SetClipboardText(txt)<0)
- THROW_ERRORF("clipboardSetText(): \"%s\"", SDL_GetError());
- }
- bool showCursor(s32 toggle){
- s32 result = SDL_ShowCursor((toggle < 0) ? -1 : toggle&1);
- if(result < 0)
- THROW_ERRORF("showCursor(): \"%s\"", SDL_GetError());
- return (bool)result;
- }
- void warpMouseGlobal(s32 x, s32 y){
- if(SDL_WarpMouseGlobal(x, y) < 0)
- THROW_ERRORF("warpMouseGlobal(): \"%s\"", SDL_GetError());
- }
- bool getRelativeMouseMode(){
- return SDL_GetRelativeMouseMode() != 0;
- }
- void setRelativeMouseMode(bool enable){
- if(SDL_SetRelativeMouseMode((SDL_bool)enable) < 0)
- THROW_ERRORF("setRelativeMouseMode(): %s", SDL_GetError());
- }
- }; /* namespace kit */
- /******************************************************************************/
- /******************************************************************************/
- //"2024-11-21\src\kit_sdl2\kit_func_convertEvent.cpp":
- #include "_kit_common.hpp"
- #define ETYPE(_event_type) e_kit.common.type = _event_type
- namespace kit {
- //returns false if that sdl event is unsupported or is otherwise unknown
- bool _convertEvent(SDL_Event& e_sdl, Event& e_kit){
- e_kit.common.timestamp = e_sdl.common.timestamp;
- switch(e_sdl.type){
- case SDL_QUIT: ETYPE(KEVENT_QUIT); break;
- case SDL_DISPLAYEVENT:
- {
- switch(e_sdl.display.event){
- case SDL_DISPLAYEVENT_ORIENTATION : ETYPE(KEVENT_DISPLAY_ORIENTATION ); break;
- case SDL_DISPLAYEVENT_CONNECTED : ETYPE(KEVENT_DISPLAY_CONNECTED ); break;
- case SDL_DISPLAYEVENT_DISCONNECTED: ETYPE(KEVENT_DISPLAY_DISCONNECTED); break;
- case SDL_DISPLAYEVENT_MOVED : ETYPE(KEVENT_DISPLAY_MOVED ); break;
- default : return false; //this shouldn't happen
- }
- e_kit.display.id = e_sdl.display.display;
- e_kit.display.data = e_sdl.display.data1;
- } break;
- case SDL_WINDOWEVENT:
- {
- switch(e_sdl.window.event){
- case SDL_WINDOWEVENT_SHOWN : ETYPE(KEVENT_WIN_SHOWN ); break;
- case SDL_WINDOWEVENT_HIDDEN : ETYPE(KEVENT_WIN_HIDDEN ); break;
- case SDL_WINDOWEVENT_EXPOSED : ETYPE(KEVENT_WIN_EXPOSED ); break;
- case SDL_WINDOWEVENT_MOVED : ETYPE(KEVENT_WIN_MOVED ); break;
- case SDL_WINDOWEVENT_RESIZED : ETYPE(KEVENT_WIN_RESIZED ); break;
- case SDL_WINDOWEVENT_SIZE_CHANGED : ETYPE(KEVENT_WIN_SIZE_CHANGED ); break;
- case SDL_WINDOWEVENT_MINIMIZED : ETYPE(KEVENT_WIN_MINIMIZED ); break;
- case SDL_WINDOWEVENT_MAXIMIZED : ETYPE(KEVENT_WIN_MAXIMIZED ); break;
- case SDL_WINDOWEVENT_RESTORED : ETYPE(KEVENT_WIN_RESTORED ); break;
- case SDL_WINDOWEVENT_ENTER : ETYPE(KEVENT_WIN_MFOCUS_GAINED ); break;
- case SDL_WINDOWEVENT_LEAVE : ETYPE(KEVENT_WIN_MFOCUS_LOST ); break;
- case SDL_WINDOWEVENT_FOCUS_GAINED : ETYPE(KEVENT_WIN_KFOCUS_GAINED ); break;
- case SDL_WINDOWEVENT_FOCUS_LOST : ETYPE(KEVENT_WIN_KFOCUS_LOST ); break;
- case SDL_WINDOWEVENT_CLOSE : ETYPE(KEVENT_WIN_CLOSE ); break;
- case SDL_WINDOWEVENT_TAKE_FOCUS : ETYPE(KEVENT_WIN_TAKE_FOCUS ); break;
- case SDL_WINDOWEVENT_HIT_TEST : ETYPE(KEVENT_WIN_HIT_TEST ); break;
- case SDL_WINDOWEVENT_ICCPROF_CHANGED: ETYPE(KEVENT_WIN_ICCPROF_CHANGED); break;
- case SDL_WINDOWEVENT_DISPLAY_CHANGED: ETYPE(KEVENT_WIN_DISPLAY_CHANGED); break;
- default : return false; //this shouldn't happen
- }
- e_kit.win.id = e_sdl.window.windowID;
- e_kit.win.data1 = e_sdl.window.data1;
- e_kit.win.data2 = e_sdl.window.data2;
- } break;
- case SDL_KEYDOWN: ETYPE(KEVENT_KEY_DOWN); goto _event_key;
- case SDL_KEYUP : ETYPE(KEVENT_KEY_UP ); _event_key:
- {
- e_kit.key.window = e_sdl.key.windowID;
- e_kit.key.pressed = e_sdl.key.state == SDL_PRESSED;
- e_kit.key.repeat = e_sdl.key.repeat != 0;
- e_kit.key.vkey = e_sdl.key.keysym.sym;
- e_kit.key.pkey = e_sdl.key.keysym.scancode;
- e_kit.key.kmods = e_sdl.key.keysym.mod;
- } break;
- //not to be confused with key events, as they are separate
- case SDL_KEYMAPCHANGED: ETYPE(KEVENT_KEYMAPCHANGED); break;
- case SDL_MOUSEMOTION : ETYPE(KEVENT_MOUSE_MOVED);
- {
- e_kit.mouse.button = e_sdl.motion.state;
- e_kit.mouse.pressed = e_kit.mouse.button != 0;
- e_kit.mouse.x = e_sdl.motion.x;
- e_kit.mouse.y = e_sdl.motion.y;
- e_kit.mouse.dx = e_sdl.motion.xrel;
- e_kit.mouse.dy = e_sdl.motion.yrel;
- } goto _set_mouse_common;
- case SDL_MOUSEBUTTONDOWN: ETYPE(KEVENT_MOUSE_DOWN); goto _event_mouse_bt;
- case SDL_MOUSEBUTTONUP : ETYPE(KEVENT_MOUSE_UP ); _event_mouse_bt:
- {
- //(turn button index into button flag using SDL_BUTTON)
- e_kit.mouse.button = SDL_BUTTON(e_sdl.button.button);
- e_kit.mouse.pressed = e_sdl.button.state == SDL_PRESSED;
- e_kit.mouse.dblClick = ((MAX(1,e_sdl.button.clicks)-1)&1) > 0;
- e_kit.mouse.x = e_sdl.button.x;
- e_kit.mouse.y = e_sdl.button.y;
- } goto _set_mouse_common;
- case SDL_MOUSEWHEEL : ETYPE(KEVENT_MOUSE_WHEEL);
- {
- e_kit.mouse.flipped = e_sdl.wheel.direction == SDL_MOUSEWHEEL_FLIPPED;
- e_kit.mouse.x = e_sdl.wheel.mouseX;
- e_kit.mouse.y = e_sdl.wheel.mouseY;
- e_kit.mouse.dx = e_sdl.wheel.x;
- e_kit.mouse.dy = e_sdl.wheel.y;
- e_kit.mouse.pdx = e_sdl.wheel.preciseX;
- e_kit.mouse.pdy = e_sdl.wheel.preciseY;
- } //goto _set_mouse_common; //(redundant)
- _set_mouse_common: //(common to all Mouse events)
- {
- e_kit.mouse.window = e_sdl.motion.windowID;
- } break;
- case SDL_JOYAXISMOTION : ETYPE(KEVENT_JOY_AXIS );
- {
- e_kit.joy.axis.which = e_sdl.jaxis.axis;
- e_kit.joy.axis.value = e_sdl.jaxis.value;
- } goto _set_joy_common;
- case SDL_JOYBALLMOTION : ETYPE(KEVENT_JOY_TRACKBALL );
- {
- e_kit.joy.trackball.which = e_sdl.jball.ball;
- e_kit.joy.trackball.dx = e_sdl.jball.xrel;
- e_kit.joy.trackball.dy = e_sdl.jball.yrel;
- } goto _set_joy_common;
- case SDL_JOYHATMOTION : ETYPE(KEVENT_JOY_HAT );
- {
- e_kit.joy.hat.which = e_sdl.jhat.hat;
- e_kit.joy.hat.value = e_sdl.jhat.value;
- } goto _set_joy_common;
- case SDL_JOYBUTTONDOWN : ETYPE(KEVENT_JOY_BUTTON_DOWN ); goto _event_joy_bt;
- case SDL_JOYBUTTONUP : ETYPE(KEVENT_JOY_BUTTON_UP ); _event_joy_bt:
- {
- e_kit.joy.button.which = e_sdl.jbutton.button;
- e_kit.joy.button.pressed = e_sdl.jbutton.state == SDL_PRESSED;
- } goto _set_joy_common;
- case SDL_JOYDEVICEADDED : ETYPE(KEVENT_JOY_DEVICE_ADDED ); goto _event_joy_dv;
- case SDL_JOYDEVICEREMOVED : ETYPE(KEVENT_JOY_DEVICE_REMOVED); _event_joy_dv:
- {
- e_kit.joy.device.added = e_sdl.jdevice.type == SDL_JOYDEVICEADDED;
- } goto _set_joy_common;
- case SDL_JOYBATTERYUPDATED: ETYPE(KEVENT_JOY_BATTERY );
- {
- e_kit.joy.battery.level = e_sdl.jbattery.level;
- } //goto _set_joy_common; //redundant
- _set_joy_common: //(common to all Joystick events)
- {
- e_kit.joy.id = e_sdl.jdevice.which;
- } break;
- case SDL_CONTROLLERAXISMOTION : ETYPE(KEVENT_CTLR_AXIS );
- {
- e_kit.ctlr.axis.which = e_sdl.caxis.axis;
- e_kit.ctlr.axis.value = e_sdl.caxis.value;
- } goto _set_ctlr_common;
- case SDL_CONTROLLERBUTTONDOWN : ETYPE(KEVENT_CTLR_BUTTON_DOWN ); goto _event_ctlr_bt;
- case SDL_CONTROLLERBUTTONUP : ETYPE(KEVENT_CTLR_BUTTON_UP ); _event_ctlr_bt:
- {
- e_kit.ctlr.button.which = e_sdl.cbutton.button;
- e_kit.ctlr.button.pressed = e_sdl.cbutton.state == SDL_PRESSED;
- } goto _set_ctlr_common;
- case SDL_CONTROLLERDEVICEADDED : ETYPE(KEVENT_CTLR_DEVICE_ADDED ); goto _event_ctlr_dv;
- case SDL_CONTROLLERDEVICEREMOVED : ETYPE(KEVENT_CTLR_DEVICE_REMOVED ); goto _event_ctlr_dv;
- case SDL_CONTROLLERDEVICEREMAPPED: ETYPE(KEVENT_CTLR_DEVICE_REMAPPED); _event_ctlr_dv:
- {
- e_kit.ctlr.device.subtype = KIT_SUBEVENT_ID(e_kit.type);
- } goto _set_ctlr_common;
- case SDL_CONTROLLERTOUCHPADDOWN : ETYPE(KEVENT_CTLR_TOUCHPAD_DOWN ); goto _event_ctlr_tp;
- case SDL_CONTROLLERTOUCHPADMOTION: ETYPE(KEVENT_CTLR_TOUCHPAD_MOVED ); goto _event_ctlr_tp;
- case SDL_CONTROLLERTOUCHPADUP : ETYPE(KEVENT_CTLR_TOUCHPAD_UP ); _event_ctlr_tp:
- {
- e_kit.ctlr.touchpad.which = e_sdl.ctouchpad.touchpad;
- e_kit.ctlr.touchpad.finger = e_sdl.ctouchpad.finger;
- e_kit.ctlr.touchpad.x = e_sdl.ctouchpad.x;
- e_kit.ctlr.touchpad.y = e_sdl.ctouchpad.y;
- e_kit.ctlr.touchpad.pressure = e_sdl.ctouchpad.pressure;
- } goto _set_ctlr_common;
- case SDL_CONTROLLERSENSORUPDATE : ETYPE(KEVENT_CTLR_SENSOR );
- {
- e_kit.ctlr.sensor.which = e_sdl.csensor.sensor;
- e_kit.ctlr.sensor.data[0] = e_sdl.csensor.data[0];
- e_kit.ctlr.sensor.data[1] = e_sdl.csensor.data[1];
- e_kit.ctlr.sensor.data[2] = e_sdl.csensor.data[2];
- e_kit.ctlr.sensor.timestamp_us = e_sdl.csensor.timestamp_us;
- } //goto _set_ctlr_common; //(redundant)
- _set_ctlr_common: //(common to all GameController events)
- {
- e_kit.ctlr.id = e_sdl.cdevice.which;
- } break;
- case SDL_CLIPBOARDUPDATE: ETYPE(KEVENT_CLIPBOARDUPDATE); break;
- case SDL_DROPFILE : ETYPE(KEVENT_DROP_FILE ); goto _event_drop;
- case SDL_DROPTEXT : ETYPE(KEVENT_DROP_TEXT ); goto _event_drop;
- case SDL_DROPBEGIN : ETYPE(KEVENT_DROP_BEGIN ); goto _event_drop;
- case SDL_DROPCOMPLETE: ETYPE(KEVENT_DROP_COMPLETE); _event_drop:
- {
- e_kit.drop.window = e_sdl.drop.windowID;
- e_kit.drop.filePath = e_sdl.drop.file;
- } break;
- case SDL_AUDIODEVICEADDED : ETYPE(KEVENT_ADEV_ADDED ); goto _event_adev;
- case SDL_AUDIODEVICEREMOVED: ETYPE(KEVENT_ADEV_REMOVED); _event_adev:
- {
- e_kit.adev.id = e_sdl.adevice.which;
- e_kit.adev.isInput = e_sdl.adevice.iscapture;
- } break;
- case SDL_RENDER_TARGETS_RESET: ETYPE(KEVENT_RENDER_TARGETS_RESET); break;
- case SDL_RENDER_DEVICE_RESET : ETYPE(KEVENT_RENDER_DEVICE_RESET ); break;
- case SDL_USEREVENT: ETYPE(KEVENT_USER);
- {
- e_kit.user.window = e_sdl.user.windowID;
- e_kit.user.id = e_sdl.user.code;
- e_kit.user.data1 = e_sdl.user.data1;
- e_kit.user.data2 = e_sdl.user.data2;
- } break;
- //event is not recognized, so return false
- default: return false;
- }
- return true;
- }
- }; /* namespace kit */
- /******************************************************************************/
- /******************************************************************************/
- //"2024-11-21\src\kit_sdl2\kit_func_CPUCapabilities.cpp":
- #include "_kit_common.hpp"
- namespace kit {
- u32 getCPUCapabilities(){
- if(!_gl.CPUCapabilities){
- if(SDL_HasRDTSC ()) _gl.CPUCapabilities |= CPU_HAS_RDTSC;
- if(SDL_HasAltiVec()) _gl.CPUCapabilities |= CPU_HAS_ALTIVEC;
- if(SDL_HasMMX ()) _gl.CPUCapabilities |= CPU_HAS_MMX;
- if(SDL_Has3DNow ()) _gl.CPUCapabilities |= CPU_HAS_3DNOW;
- if(SDL_HasSSE ()) _gl.CPUCapabilities |= CPU_HAS_SSE;
- if(SDL_HasSSE2 ()) _gl.CPUCapabilities |= CPU_HAS_SSE2;
- if(SDL_HasSSE3 ()) _gl.CPUCapabilities |= CPU_HAS_SSE3;
- if(SDL_HasSSE41 ()) _gl.CPUCapabilities |= CPU_HAS_SSE41;
- if(SDL_HasSSE42 ()) _gl.CPUCapabilities |= CPU_HAS_SSE42;
- if(SDL_HasAVX ()) _gl.CPUCapabilities |= CPU_HAS_AVX;
- if(SDL_HasAVX512F()) _gl.CPUCapabilities |= CPU_HAS_AVX512F;
- if(SDL_HasARMSIMD()) _gl.CPUCapabilities |= CPU_HAS_ARMSIMD;
- if(SDL_HasNEON ()) _gl.CPUCapabilities |= CPU_HAS_NEON;
- if(SDL_HasLSX ()) _gl.CPUCapabilities |= CPU_HAS_LSX;
- if(SDL_HasLASX ()) _gl.CPUCapabilities |= CPU_HAS_LASX;
- }
- return _gl.CPUCapabilities;
- }
- u32 getCPUCacheLineSize(){
- return SDL_GetCPUCacheLineSize();
- }
- u32 getNumLogicalCPUCores(){
- return SDL_GetCPUCount();
- }
- }; /* namespace kit */
- /******************************************************************************/
- /******************************************************************************/
- //"2024-11-21\src\kit_sdl2\kit_func_init-quit.cpp":
- #include "_kit_common.hpp"
- namespace kit {
- _globalStates _gl;
- const char _fstr_failure[] = "(FSTR FAILED)";
- const char _boolStr_false[] = "false";
- const char _boolStr_true[] = "true";
- #define FLAG_SET(_flag) (flags&(_flag))
- #define SHOULD_INIT(_flag, _member) (FLAG_SET(_flag) && !_gl.init._member)
- #define INIT_CHECK(_sdl_flag) { \
- if(SDL_InitSubSystem(_sdl_flag)<0){ THROW_ERRORF("initSubsystems(): \"%s\"", SDL_GetError()); } }
- #define KINIT_JOYSTICK_IMPLICIT (0x40)
- #define KINIT_EVENTS_IMPLICIT (0x80)
- void initSubsystems(u32 flags){
- SDL_version ver;
- SDL_GetVersion(&ver);
- if(ver.major != SDL_MAJOR_VERSION){
- THROW_ERRORF("kit_sdl%u requires SDL%u, not SDL%u",
- SDL_MAJOR_VERSION, SDL_MAJOR_VERSION, ver.major);
- }
- if(ver.minor < SDL_MINOR_VERSION) {
- THROW_ERRORF("SDL%u version must be >= %u.%u.x, not %u.%u.x",
- SDL_MAJOR_VERSION,
- SDL_MAJOR_VERSION, SDL_MINOR_VERSION,
- ver.major, ver.minor);
- }
- //just warn instead of error if patch level is less than what it should be
- if(ver.patch < SDL_PATCHLEVEL && ver.minor == SDL_MINOR_VERSION &&
- !_gl.patch_warning_disabled)
- {
- _log("\b\b\b\b\b\bWARN: SDL%u patch version should be >= x.x.%u, not x.x.%u",
- SDL_MAJOR_VERSION, SDL_PATCHLEVEL, ver.patch);
- }
- //disable these flags just in case the user set them for whatever reason
- flags &= ~(KINIT_JOYSTICK_IMPLICIT|KINIT_EVENTS_IMPLICIT);
- if(SHOULD_INIT(KINIT_TIMER, timer)){
- INIT_CHECK(SDL_INIT_TIMER);
- _gl.init.timer = true;
- }
- if(SHOULD_INIT(KINIT_AUDIO, audio)){
- INIT_CHECK(SDL_INIT_AUDIO);
- _gl.init.audio = true;
- }
- if(SHOULD_INIT(KINIT_VIDEO, video)){
- INIT_CHECK(SDL_INIT_VIDEO);
- _gl.init.video = true;
- flags |= KINIT_EVENTS_IMPLICIT; //indicate that events should auto-init
- }
- if(SHOULD_INIT(KINIT_GAMECONTROLLER, gamecontroller)){
- INIT_CHECK(SDL_INIT_GAMECONTROLLER);
- _gl.init.gamecontroller = true;
- flags |= KINIT_JOYSTICK_IMPLICIT; //indicate that joystick should auto-init
- }
- if(SHOULD_INIT(KINIT_JOYSTICK|KINIT_JOYSTICK_IMPLICIT, joystick)){
- if(FLAG_SET(KINIT_JOYSTICK)){
- INIT_CHECK(SDL_INIT_JOYSTICK);
- _gl.init.joystick_sdl = true;
- }
- _gl.init.joystick = true;
- flags |= KINIT_EVENTS_IMPLICIT; //indicate that events should auto-init
- }
- if(SHOULD_INIT(KINIT_EVENTS|KINIT_EVENTS_IMPLICIT, events)){
- if(FLAG_SET(KINIT_EVENTS)){
- INIT_CHECK(SDL_INIT_EVENTS);
- _gl.init.events_sdl = true;
- }
- _gl.init.events = true;
- }
- bool errors_just_created = false;
- if(flags && _gl.errors == nullptr){
- _gl.errors_len = 1;
- _gl.errors = (Error*)memory::alloc( sizeof(Error) * _gl.errors_len );
- if(_gl.errors == nullptr){
- _gl.errors_len = 0;
- THROW_ERROR("initSubsystems(): failed to allocate internal memory");
- }
- errors_just_created = true;
- memory::set(_gl.errors, 0, sizeof(Error) * _gl.errors_len );
- }
- if(_gl.init_value && _gl.lock == nullptr){
- _gl.lock = SDL_CreateMutex();
- if(_gl.lock == nullptr){
- if(errors_just_created) _freeErrors();
- THROW_ERROR("initSubsystems(): failed to create internal mutex");
- }
- }
- }
- #define SHOULD_QUIT(_flag, _member) (FLAG_SET(_flag) && _gl.init._member)
- #define QUIT_SUB(_flag) SDL_QuitSubSystem(_flag)
- void quitSubsystems(u32 flags){
- flags &= ~(KINIT_JOYSTICK_IMPLICIT|KINIT_EVENTS_IMPLICIT);
- if(SHOULD_QUIT(KINIT_TIMER, timer)){
- QUIT_SUB(SDL_INIT_TIMER);
- _gl.init.timer = false;
- }
- if(SHOULD_QUIT(KINIT_AUDIO, audio)){
- QUIT_SUB(SDL_INIT_AUDIO);
- _gl.init.audio = false;
- }
- if(SHOULD_QUIT(KINIT_VIDEO, video)){
- QUIT_SUB(SDL_INIT_VIDEO);
- _gl.init.video = false;
- }
- if(SHOULD_QUIT(KINIT_GAMECONTROLLER, gamecontroller)){
- QUIT_SUB(SDL_INIT_GAMECONTROLLER);
- _gl.init.gamecontroller = false;
- }
- if(SHOULD_QUIT(KINIT_JOYSTICK, joystick)){
- if(_gl.init.joystick_sdl){
- QUIT_SUB(SDL_INIT_JOYSTICK);
- _gl.init.joystick_sdl = false;
- }
- _gl.init.joystick = false;
- }
- if(SHOULD_QUIT(KINIT_EVENTS, events)){
- if(_gl.init.events_sdl){
- QUIT_SUB(SDL_INIT_EVENTS);
- _gl.init.events_sdl = false;
- }
- _gl.init.events = false;
- }
- if(!_gl.init_value){
- _freeErrors();
- if(_gl.lock != nullptr){
- LOCK_GLOBALS(); //to lessen the chance that the mutex is destroyed
- UNLOCK_GLOBALS(); //before all the other threads unlock it (hopefully)
- SDL_DestroyMutex(_gl.lock);
- _gl.lock = nullptr;
- }
- //apparently you still need to call SDL_Quit,
- //(even if you've already closed any open subsystems)
- SDL_Quit();
- }
- }
- }; /* namespace kit */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement