Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************************************************/
- /******************************************************************************/
- //"ksdl2\src\kit_sdl2\kit_fileio_File.cpp":
- #include "_kit_common.hpp"
- #define KIT_IS_INVALID_FILE (!_valid && !_constructing)
- #define KIT_FILE_CHECK(_funcName) \
- if(KIT_IS_INVALID_FILE){ throw PUSH_ERROR(_funcName ": invalid File"); } \
- if(_file == nullptr ){ throw PUSH_ERROR(_funcName ": _file = nullptr"); } \
- if(_closed ){ throw PUSH_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){
- _type = KIT_CLASSTYPE_FILE;
- _file = (GenOpqPtr)SDL_RWFromFile(filePath, mode);
- if(_file == nullptr) throw PUSH_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 PUSH_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 PUSH_ERRORF("File::write(): \"%s\"", SDL_GetError());
- }
- size_t File::seek(s64 offset, u32 whence){
- KIT_FILE_CHECK("File::seek()");
- if(whence > 2) throw PUSH_ERROR("File::seek(): whence is invalid");
- s64 result = FILE_PTR->SEEK(offset, whence);
- if(result < 0) throw PUSH_ERRORF("File::seek(): \"%s\"", SDL_GetError());
- return (size_t)result;
- }
- void File::close(){
- KIT_FILE_CHECK("File::close()");
- if(FILE_PTR->CLOSE()<0)
- throw PUSH_ERROR("File::close(): write error occurred while flushing data");
- }
- size_t File::size(){
- KIT_FILE_CHECK("File::size()");
- s64 result = FILE_PTR->SIZE();
- if(result < 0)
- throw PUSH_ERROR("File::size(): failed to get file's size");
- return (size_t)result;
- }
- }; /* namespace kit */
- /******************************************************************************/
- /******************************************************************************/
- //"ksdl2\src\kit_sdl2\kit_func.cpp":
- #include "_kit_common.hpp"
- namespace kit {
- //(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 PUSH_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 PUSH_ERROR("strnCmp(): str_a = nullptr");
- //if(str_b == nullptr) throw PUSH_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);
- }
- const char* getLastSysError(){
- return SDL_GetError();
- }
- void clearSysError(){
- SDL_ClearError();
- }
- void freeThreadError(){
- _freeError(); //frees error of *current* thread
- }
- u32 getNumLogicalCPUCores(){
- return SDL_GetCPUCount();
- }
- u32 getCPUCacheLineSize(){
- return SDL_GetCPUCacheLineSize();
- }
- #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 PUSH_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 PUSH_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 PUSH_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 PUSH_ERRORF("clipboardSetText(): \"%s\"", SDL_GetError());
- }
- bool showCursor(s32 toggle){
- s32 result = SDL_ShowCursor((toggle < 0) ? -1 : toggle&1);
- if(result < 0)
- throw PUSH_ERRORF("showCursor(): \"%s\"", SDL_GetError());
- return (bool)result;
- }
- void warpMouseGlobal(s32 x, s32 y){
- if(SDL_WarpMouseGlobal(x, y) < 0)
- throw PUSH_ERRORF("warpMouseGlobal(): \"%s\"", SDL_GetError());
- }
- }; /* namespace kit */
- /******************************************************************************/
- /******************************************************************************/
- //"ksdl2\src\kit_sdl2\kit_func_init-quit.cpp":
- #include "_kit_common.hpp"
- namespace kit {
- _globalStates _gl;
- const char _fstr_failure[] = "(FSTR FAILED)";
- #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 PUSH_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 PUSH_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 PUSH_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 PUSH_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 PUSH_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
- 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 */
- /******************************************************************************/
- /******************************************************************************/
- //"ksdl2\src\kit_sdl2\kit_func_pollEvent.cpp":
- #include "_kit_common.hpp"
- namespace kit {
- #define ETYPE(_event_type) e_kit.common.type = _event_type
- bool pollEvent(Event* event_p){
- if(!_gl.init.events)
- throw PUSH_ERROR("pollEvent(): events subsystem is uninitialized");
- SDL_Event e_sdl;
- Event e_kit;
- bool queueWasNotEmpty;
- _get_another_event:
- queueWasNotEmpty = SDL_PollEvent((event_p != nullptr) ? &e_sdl : nullptr);
- if(event_p == nullptr) return queueWasNotEmpty;
- e_kit.common.timestamp = e_sdl.common.timestamp;
- if(queueWasNotEmpty)
- 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 : goto _get_another_event; //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 : goto _get_another_event; //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.filePath = e_sdl.drop.file;
- e_kit.drop.window = e_sdl.drop.windowID;
- } 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 call SDL_PollEvent until either
- //a supported event type is found, or event queue empties
- default: goto _get_another_event;
- }
- *event_p = e_kit; //should be impossible for event_p to be null here
- return queueWasNotEmpty;
- }
- const char* getEventText(u32 type){
- const char* eventName;
- #ifdef _DEBUG
- switch(type){
- case KEVENT_NULL : eventName = "KEVENT_NULL"; break;
- case KEVENT_COMMON : eventName = "KEVENT_COMMON"; break;
- case KEVENT_DISPLAY : eventName = "KEVENT_DISPLAY"; break;
- case KEVENT_DISPLAY_ORIENTATION : eventName = "KEVENT_DISPLAY_ORIENTATION"; break;
- case KEVENT_DISPLAY_DISCONNECTED: eventName = "KEVENT_DISPLAY_DISCONNECTED"; break;
- case KEVENT_DISPLAY_CONNECTED : eventName = "KEVENT_DISPLAY_CONNECTED"; break;
- case KEVENT_DISPLAY_MOVED : eventName = "KEVENT_DISPLAY_MOVED"; break;
- case KEVENT_WIN : eventName = "KEVENT_WIN"; break;
- case KEVENT_WIN_EXPOSED : eventName = "KEVENT_WIN_EXPOSED"; break;
- case KEVENT_WIN_HIDDEN : eventName = "KEVENT_WIN_HIDDEN"; break;
- case KEVENT_WIN_SHOWN : eventName = "KEVENT_WIN_SHOWN"; break;
- case KEVENT_WIN_MOVED : eventName = "KEVENT_WIN_MOVED"; break;
- case KEVENT_WIN_RESIZED : eventName = "KEVENT_WIN_RESIZED"; break;
- case KEVENT_WIN_SIZE_CHANGED : eventName = "KEVENT_WIN_SIZE_CHANGED"; break;
- case KEVENT_WIN_RESTORED : eventName = "KEVENT_WIN_RESTORED"; break;
- case KEVENT_WIN_MINIMIZED : eventName = "KEVENT_WIN_MINIMIZED"; break;
- case KEVENT_WIN_MAXIMIZED : eventName = "KEVENT_WIN_MAXIMIZED"; break;
- case KEVENT_WIN_MFOCUS_LOST : eventName = "KEVENT_WIN_MFOCUS_LOST"; break;
- case KEVENT_WIN_MFOCUS_GAINED : eventName = "KEVENT_WIN_MFOCUS_GAINED"; break;
- case KEVENT_WIN_KFOCUS_LOST : eventName = "KEVENT_WIN_KFOCUS_LOST"; break;
- case KEVENT_WIN_KFOCUS_GAINED : eventName = "KEVENT_WIN_KFOCUS_GAINED"; break;
- case KEVENT_WIN_CLOSE : eventName = "KEVENT_WIN_CLOSE"; break;
- case KEVENT_WIN_TAKE_FOCUS : eventName = "KEVENT_WIN_TAKE_FOCUS"; break;
- case KEVENT_WIN_HIT_TEST : eventName = "KEVENT_WIN_HIT_TEST"; break;
- case KEVENT_WIN_ICCPROF_CHANGED : eventName = "KEVENT_WIN_ICCPROF_CHANGED"; break;
- case KEVENT_WIN_DISPLAY_CHANGED : eventName = "KEVENT_WIN_DISPLAY_CHANGED"; break;
- case KEVENT_KEY : eventName = "KEVENT_KEY"; break;
- case KEVENT_KEY_DOWN : eventName = "KEVENT_KEY_DOWN"; break;
- case KEVENT_KEY_UP : eventName = "KEVENT_KEY_UP"; break;
- case KEVENT_KEYMAPCHANGED : eventName = "KEVENT_KEYMAPCHANGED"; break;
- case KEVENT_MOUSE : eventName = "KEVENT_MOUSE"; break;
- case KEVENT_MOUSE_MOVED : eventName = "KEVENT_MOUSE_MOVED"; break;
- case KEVENT_MOUSE_UP : eventName = "KEVENT_MOUSE_UP"; break;
- case KEVENT_MOUSE_DOWN : eventName = "KEVENT_MOUSE_DOWN"; break;
- case KEVENT_MOUSE_WHEEL : eventName = "KEVENT_MOUSE_WHEEL"; break;
- case KEVENT_JOY : eventName = "KEVENT_JOY"; break;
- case KEVENT_JOY_AXIS : eventName = "KEVENT_JOY_AXIS"; break;
- case KEVENT_JOY_TRACKBALL : eventName = "KEVENT_JOY_TRACKBALL"; break;
- case KEVENT_JOY_HAT : eventName = "KEVENT_JOY_HAT"; break;
- case KEVENT_JOY_BUTTON_UP : eventName = "KEVENT_JOY_BUTTON_UP"; break;
- case KEVENT_JOY_BUTTON_DOWN : eventName = "KEVENT_JOY_BUTTON_DOWN"; break;
- case KEVENT_JOY_DEVICE_REMOVED : eventName = "KEVENT_JOY_DEVICE_REMOVED"; break;
- case KEVENT_JOY_DEVICE_ADDED : eventName = "KEVENT_JOY_DEVICE_ADDED"; break;
- case KEVENT_JOY_BATTERY : eventName = "KEVENT_JOY_BATTERY"; break;
- case KEVENT_CTLR : eventName = "KEVENT_CTLR"; break;
- case KEVENT_CTLR_AXIS : eventName = "KEVENT_CTLR_AXIS"; break;
- case KEVENT_CTLR_BUTTON_UP : eventName = "KEVENT_CTLR_BUTTON_UP"; break;
- case KEVENT_CTLR_BUTTON_DOWN : eventName = "KEVENT_CTLR_BUTTON_DOWN"; break;
- case KEVENT_CTLR_DEVICE_REMOVED : eventName = "KEVENT_CTLR_DEVICE_REMOVED"; break;
- case KEVENT_CTLR_DEVICE_ADDED : eventName = "KEVENT_CTLR_DEVICE_ADDED"; break;
- case KEVENT_CTLR_DEVICE_REMAPPED: eventName = "KEVENT_CTLR_DEVICE_REMAPPED"; break;
- case KEVENT_CTLR_TOUCHPAD_UP : eventName = "KEVENT_CTLR_TOUCHPAD_UP"; break;
- case KEVENT_CTLR_TOUCHPAD_DOWN : eventName = "KEVENT_CTLR_TOUCHPAD_DOWN"; break;
- case KEVENT_CTLR_TOUCHPAD_MOVED : eventName = "KEVENT_CTLR_TOUCHPAD_MOVED"; break;
- case KEVENT_CTLR_SENSOR : eventName = "KEVENT_CTLR_SENSOR"; break;
- case KEVENT_ADEV : eventName = "KEVENT_ADEV"; break;
- case KEVENT_ADEV_ADDED : eventName = "KEVENT_ADEV_ADDED"; break;
- case KEVENT_ADEV_REMOVED : eventName = "KEVENT_ADEV_REMOVED"; break;
- case KEVENT_DROP : eventName = "KEVENT_DROP"; break;
- case KEVENT_DROP_FILE : eventName = "KEVENT_DROP_FILE"; break;
- case KEVENT_DROP_TEXT : eventName = "KEVENT_DROP_TEXT"; break;
- case KEVENT_DROP_BEGIN : eventName = "KEVENT_DROP_BEGIN"; break;
- case KEVENT_DROP_COMPLETE : eventName = "KEVENT_DROP_COMPLETE"; break;
- case KEVENT_QUIT : eventName = "KEVENT_QUIT"; break;
- case KEVENT_USER : eventName = "KEVENT_USER"; break;
- case KEVENT_RENDER : eventName = "KEVENT_RENDER"; break;
- case KEVENT_RENDER_TARGETS_RESET: eventName = "KEVENT_RENDER_TARGETS_RESET"; break;
- case KEVENT_RENDER_DEVICE_RESET : eventName = "KEVENT_RENDER_DEVICE_RESET "; break;
- case KEVENT_CLIPBOARDUPDATE : eventName = "KEVENT_CLIPBOARDUPDATE"; break;
- default : eventName = "KEVENT_UNKNOWN";
- }
- #else
- type = type; //makes the unused variable warning go away without using #pragma
- eventName = "(getEventText() is not available in release build)";
- #endif /* _DEBUG */
- return eventName;
- }
- }; /* namespace kit */
- /******************************************************************************/
- /******************************************************************************/
- //"ksdl2\src\kit_sdl2\kit_lodepng_custom.cpp":
- #include "_kit_common.hpp"
- using namespace kit;
- void* lodepng_malloc(size_t size){
- #ifdef LODEPNG_MAX_ALLOC
- if(size > LODEPNG_MAX_ALLOC) return 0;
- #endif
- return memory::alloc(size);
- }
- // NOTE: when realloc returns NULL, it leaves the original memory untouched
- void* lodepng_realloc(void* ptr, size_t new_size){
- #ifdef LODEPNG_MAX_ALLOC
- if(new_size > LODEPNG_MAX_ALLOC) return 0;
- #endif
- return memory::realloc(&ptr, new_size);
- }
- void lodepng_free(void* ptr){
- memory::free(&ptr);
- }
- //i rewrote lodepng's file io functions to only use SDL functions
- #ifndef LODEPNG_NO_COMPILE_DISK
- #undef LONG_MAX
- #define LONG_MAX (2147483647)
- unsigned lodepng_load_file(unsigned char** out, size_t* outsize, const char* filename) {
- *out = (unsigned char*)SDL_LoadFile(filename, outsize);
- if(*out == nullptr){
- return 78;
- } else {
- ++numAllocations; //a subsequent call to memory::free should undo this
- return 0;
- }
- }
- // write given buffer to the file, overwriting the file, it doesn't append to it.
- unsigned lodepng_save_file(const unsigned char* buffer, size_t buffersize, const char* filename) {
- SDL_RWops* file = SDL_RWFromFile(filename, "wb");
- if(file == nullptr) return 79;
- size_t bytesWritten = SDL_RWwrite(file, buffer, 1, buffersize);
- SDL_RWclose(file);
- if(bytesWritten < buffersize) return 79;
- else return 0;
- }
- /*
- #undef LONG_MAX
- #define LONG_MAX (2147483647)
- #include <stdio.h>
- // returns negative value on error. This should be pure C compatible, so no fstat.
- static long lodepng_filesize(const char* filename) {
- FILE* file;
- long size;
- file = fopen(filename, "rb");
- if(!file) return -1;
- if(fseek(file, 0, SEEK_END) != 0) {
- fclose(file);
- return -1;
- }
- size = ftell(file);
- // It may give LONG_MAX as directory size, this is invalid for us.
- if(size == LONG_MAX) size = -1;
- fclose(file);
- return size;
- }
- // load file into buffer that already has the correct allocated size. Returns error code.
- static unsigned lodepng_buffer_file(unsigned char* out, size_t size, const char* filename) {
- FILE* file;
- size_t readsize;
- file = fopen(filename, "rb");
- if(!file) return 78;
- readsize = fread(out, 1, size, file);
- fclose(file);
- if(readsize != size) return 78;
- return 0;
- }
- unsigned lodepng_load_file(unsigned char** out, size_t* outsize, const char* filename) {
- long size = lodepng_filesize(filename);
- if(size < 0) return 78;
- *outsize = (size_t)size;
- *out = (unsigned char*)lodepng_malloc((size_t)size);
- if(!(*out) && size > 0) return 83; // the above malloc failed
- return lodepng_buffer_file(*out, (size_t)size, filename);
- }
- // write given buffer to the file, overwriting the file, it doesn't append to it.
- unsigned lodepng_save_file(const unsigned char* buffer, size_t buffersize, const char* filename) {
- FILE* file;
- file = fopen(filename, "wb" );
- if(!file) return 79;
- fwrite(buffer, 1, buffersize, file);
- fclose(file);
- return 0;
- }
- */
- #endif /* LODEPNG_NO_COMPILE_DISK */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement