Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************************************************/
- /******************************************************************************/
- //"2024-11-21\src\kit_sdl2\kit_Surface_LoadAllTypes.cpp":
- //hopefully by putting this into another object file, you can have the choice
- //of only using one image type while only linking code of that specific type
- #include "_kit_common.hpp"
- namespace kit {
- //file signatures
- #define MAGIC_BMP 0x4D42 // = "BM"
- #define MAGIC_QOI 0x66696F71 // = "qoif"
- #define MAGIC_PNG 0x0A1A0A0D474E5089 // = "\x89PNG\x0D\x0A\x1A\x0A"
- Surface::Surface(const char* filePath){
- if(filePath == nullptr)
- THROW_ERROR("Surface::Surface(any file format): filePath = nullptr");
- if(!fileio::exists(filePath)){
- THROW_ERRORF("Surface::Surface(any file format): \"%s\" doesn't exist",
- filePath);
- }
- u64 magic; //file signature, up to a maximum of 8 bytes
- if( File(filePath,"rb").read(&magic, sizeof(magic)) < sizeof(magic) )
- {
- THROW_ERRORF(
- "Surface::Surface(any file format): failed to read 8 bytes of file \"%s\"",
- filePath
- );
- }
- SurfaceLoaderCallback callback;
- //test for file signatures in order of their byte count
- if( (magic& 0xffff) == MAGIC_BMP) callback = nullptr;
- else if((magic& 0xffffffff) == MAGIC_QOI) callback = SurfaceLoadQOI;
- else if((magic&0xffffffffffffffff) == MAGIC_PNG) callback = SurfaceLoadPNG;
- else THROW_ERROR( "Surface::Surface(any file format): unsupported file format");
- //(auto-increments numAllocations)
- _construct_file(filePath, callback, "Surface::Surface(any file format)");
- }
- }; /* namespace kit */
- /******************************************************************************/
- /******************************************************************************/
- //"2024-11-21\src\kit_sdl2\kit_Surface_LoadPNG.cpp":
- #include "_kit_common.hpp"
- #include "../lodepng/lodepng.hpp"
- namespace kit {
- using namespace png;
- void* SurfaceLoadPNG(const char* filePath, u32& fmt_out,
- u31& width_out, u31& height_out)
- {
- if(!fileio::exists(filePath))
- THROW_ERRORF("SurfaceLoadPNG(): file \"%s\" doesn't exist", filePath);
- colors::ABGR* pixels;
- u32 width_in;
- u32 height_in;
- u32 err = lodepng_decode32_file((u8**)&pixels,&width_in,&height_in,filePath);
- if(err) THROW_ERRORF("SurfaceLoadPNG(): \"%s\"", lodepng_error_text(err));
- if(width_in > KIT_S32_MAX)
- THROW_ERROR("SurfaceLoadPNG(): image width > KIT_S32_MAX");
- if(height_in > KIT_S32_MAX)
- THROW_ERROR("SurfaceLoadPNG(): image height > KIT_S32_MAX");
- //analyze the input pixels to determine the pixel format (out of 2)
- //(PIXELFMT_<ABGR8888, BGR888>)
- size_t numPixels = width_in * height_in;
- bool uses_alpha = false; //will remain false if all pixels are opaque
- for(size_t i=0; i<numPixels; ++i)
- uses_alpha |= pixels[i].a < 255;
- fmt_out = (uses_alpha) ? PIXELFMT_ABGR8888 : PIXELFMT_BGR888;
- width_out = width_in;
- height_out = height_in;
- return pixels;
- }
- }; /* namespace kit */
- /******************************************************************************/
- /******************************************************************************/
- //"2024-11-21\src\kit_sdl2\kit_Surface_LoadSaveQOI.cpp":
- #include "_kit_common.hpp"
- #define QOI_NO_STDIO
- #define QOI_MALLOC(sz) kit::memory::alloc(sz)
- #define QOI_FREE(p) kit::memory::free(&p)
- #define QOI_ZEROARR(a) kit::memory::set((a),0,sizeof(a))
- namespace kit {
- namespace qoi {
- #define QOI_IMPLEMENTATION
- #include "../qoi/qoi.h"
- }; /* namespace qoi */
- //hopefully putting it in its own namespace will prevent any user naming conflicts
- //(maybe it wouldn't have been a problem anyway, idk)
- using namespace qoi;
- //returns pixel data in the form of colors::ABGR, or nullptr on failure
- //(alpha may be unused if format is BGR888)
- //if successful, *desc is filled with the image's non-pixel data info
- //set channels to 0 to use default value found in image's file data
- //result should be freed after use
- //void *qoi::qoi_decode(const void *data, int size, qoi_desc *desc, int channels);
- //data is in the form of colors::ABGR
- //(alpha may be unused if format is BGR888)
- //returns encoded qoi data, or nullptr on failure
- //desc is used as an input, unlike qoi_decode
- //if successful, out_len is set to the size of the encoded data, in bytes
- //result should be freed after use
- //void *qoi::qoi_encode(const void *data, const qoi_desc *desc, int *out_len);
- #define QOI_SIG 0x66696F71 // = "qoif"
- void* SurfaceLoadQOI(const char* filePath, u32& format_out,
- u31& width_out, u31& height_out)
- {
- if(!fileio::exists(filePath))
- THROW_ERRORF("SurfaceLoadQOI(): file \"%s\" doesn't exist", filePath);
- BinaryData _fileData(filePath);
- s32 fileSize = ( s32 )_fileData.getSize();
- void* fileData = (void*)_fileData.getData(); //the ACTUAL file data
- if(_fileData.getSize() > KIT_S32_MAX)
- THROW_ERROR("SurfaceLoadQOI(): file is too large for a QOI (>2GiB)");
- if(fileSize < 14)
- THROW_ERROR("SurfaceLoadQOI(): file is too small for a QOI (<14B)");
- if((*(u32*)fileData) != QOI_SIG)
- THROW_ERROR("SurfaceLoadQOI(): file is not a QOI image");
- qoi_desc desc;
- void* _pixels = (colors::ABGR*)qoi_decode(fileData, fileSize, &desc, 0);
- if(_pixels == nullptr)
- THROW_ERROR("SurfaceLoadQOI(): failed to decode QOI");
- if(desc.channels < 3 || desc.channels > 4){
- memory::free(&_pixels);
- THROW_ERROR("SurfaceLoadQOI(): color channels must be either 3 or 4");
- }
- if(desc.width > KIT_S32_MAX){
- memory::free(&_pixels);
- THROW_ERROR("SurfaceLoadQOI(): image width cannot be >= 2^31 pixels");
- }
- if(desc.height > KIT_S32_MAX){
- memory::free(&_pixels);
- THROW_ERROR("SurfaceLoadQOI(): image height cannot be >= 2^31 pixels");
- }
- //the only difference between ABGR8888 and BGR888 is that
- //BGR888 ignores the high byte that's usually used for the alpha channel
- //(both have a 32-bit width)
- format_out = (desc.channels == 4) ? PIXELFMT_ABGR8888 : PIXELFMT_BGR888;
- width_out = desc.width;
- height_out = desc.height;
- if(desc.channels == 4){
- //_pixels can be interpreted as a colors::ABGR*; return as-is
- return _pixels;
- } else { //desc.channels == 3
- //_pixels is made of 24-bit rgb triplets without padding,
- //so the pixel data needs to be converted to colors::ABGR
- //(where the .a component is always 255, AKA fully opaque)
- colors::BGR* pixels_in = (colors::BGR*)_pixels;
- size_t numPixels = desc.width * desc.height;
- colors::ABGR* pixels_out = (colors::ABGR*)memory::alloc(numPixels * sizeof(colors::ABGR));
- if(pixels_out == nullptr){
- memory::free(&pixels_in);
- THROW_ERROR("SurfaceLoadQOI(): failed to allocate memory for pixel data");
- }
- //copy color data accordingly
- for(size_t i=0; i<numPixels; ++i){
- pixels_out[i].r = pixels_in[i].r;
- pixels_out[i].g = pixels_in[i].g;
- pixels_out[i].b = pixels_in[i].b;
- pixels_out[i].a = 255;
- }
- memory::free(&pixels_in);
- return pixels_out;
- }
- }
- void SurfaceSaveQOI(const char* filePath, Surface& surface_in){
- shape::point size_in = surface_in.getSize();
- u32 format_in = surface_in.getPixelFmt();
- u32 width_in = size_in.x;
- u32 height_in = size_in.y;
- bool alpha_in = KIT_ISPIXELFORMAT_ALPHA(format_in);
- //create temporary surface of format PIXELFMT_ABGR8888
- Surface surface_out(surface_in, PIXELFMT_ABGR8888);
- //(doesn't need to be freed, since it's freed when surface_out is destroyed)
- colors::ABGR* pixels_in = (colors::ABGR*)surface_out.getPixelData();
- qoi_desc desc_out;
- desc_out.width = width_in;
- desc_out.height = height_in;
- desc_out.channels = (alpha_in) ? 4 : 3;
- desc_out.colorspace = 0;
- //pixels_in can be fed directly to the output if no conversion is needed
- void* pixels_out = pixels_in;
- bool intermediate_used = false;
- //only make an intermediate buffer if unpadded rgb triplets are required,
- //since they're 24-bits in length, not 32
- if(!alpha_in){
- size_t numPixels = width_in * height_in;
- pixels_out = memory::alloc(numPixels * desc_out.channels);
- intermediate_used = true;
- if(pixels_out == nullptr)
- THROW_ERROR("SurfaceSaveQOI(): failed to allocate for intermediate buffer");
- //convert ABGR to BGR
- colors::BGR* _pixels_out = (colors::BGR*)pixels_out;
- for(size_t i=0; i<numPixels; ++i){
- _pixels_out[i].r = pixels_in[i].r;
- _pixels_out[i].g = pixels_in[i].g;
- _pixels_out[i].b = pixels_in[i].b;
- }
- }
- int fileSize;
- //the point of this is that encoded_data should auto-free upon throwing
- memory::Wrapper encoded_data( qoi_encode(pixels_out, &desc_out, &fileSize) );
- //pixels_out should be freed (if alloc'd) before checking
- //to see if qoi_encode succeeded
- if(intermediate_used)
- memory::free(&pixels_out);
- if(encoded_data.ptr == nullptr)
- THROW_ERROR("SurfaceSaveQOI(): failed to encode QOI");
- if(fileSize < 0) //this should be impossible, but just in case
- THROW_ERROR("SurfaceSaveQOI(): encoded QOI data size was negative??");
- fileio::writeAll(filePath, encoded_data.ptr, (size_t)fileSize);
- }
- }; /* namespace kit */
- /******************************************************************************/
- /******************************************************************************/
- //"2024-11-21\src\kit_sdl2\kit_Surface_SavePNG.cpp":
- #include "_kit_common.hpp"
- #include "../lodepng/lodepng.hpp"
- namespace kit {
- using namespace png;
- //make sure to specifically use colors::BGR for this one, since i don't want padding
- //u32 lodepng_encode24_file(const char* filename, const u8* img, u32 w, u32 h);
- //u32 lodepng_encode32_file(const char* filename, const u8* img, u32 w, u32 h);
- void SurfaceSavePNG(const char* filePath, Surface& surface_in){
- shape::point size_in = surface_in.getSize();
- u32 format_in = surface_in.getPixelFmt();
- u32 width_in = size_in.x;
- u32 height_in = size_in.y;
- bool alpha_in = KIT_ISPIXELFORMAT_ALPHA(format_in);
- //create temporary surface of format PIXELFMT_ABGR8888
- Surface surface_out(surface_in, PIXELFMT_ABGR8888);
- //(doesn't need to be freed, since it's freed when surface_out is destroyed)
- colors::ABGR* pixels_in = (colors::ABGR*)surface_out.getPixelData();
- u32 err = 0;
- if(0){ //only entered in the event of an error related to lodepng
- _throw_lodepng_error:
- THROW_ERRORF("SurfaceSavePNG(): \"%s\"", lodepng_error_text(err));
- }
- if(alpha_in){
- //pixels_in can be fed directly to the output if no conversion is needed
- err = lodepng_encode32_file(filePath, (u8*)pixels_in, width_in, height_in);
- if(err) goto _throw_lodepng_error;
- } else { //use rgb triplets
- size_t numPixels = width_in * height_in;
- void* pixels_out = memory::alloc(numPixels * sizeof(colors::BGR));
- if(pixels_out == nullptr)
- THROW_ERROR("SurfaceSavePNG(): failed to allocate for intermediate buffer");
- //convert ABGR to BGR
- colors::BGR* _pixels_out = (colors::BGR*)pixels_out;
- for(size_t i=0; i<numPixels; ++i){
- _pixels_out[i].r = pixels_in[i].r;
- _pixels_out[i].g = pixels_in[i].g;
- _pixels_out[i].b = pixels_in[i].b;
- }
- err = lodepng_encode24_file(filePath, (u8*)pixels_out, width_in, height_in);
- memory::free(&pixels_out); //free BEFORE checking for error
- if(err) goto _throw_lodepng_error;
- }
- }
- }; /* namespace kit */
- /******************************************************************************/
- /******************************************************************************/
- //"2024-11-21\src\kit_sdl2\kit_Thread.cpp":
- #include "_kit_common.hpp"
- #define KIT_THREAD_NAME _kit_thread_name //(default thread name)
- #define KIT_INVALID_THREAD "invalid Thread" //this should remain a literal
- #define KIT_IS_INVALID_THREAD (!_valid)
- #define THREAD_PTR ((SDL_Thread*)_thread) //only applicable for Thread objects
- namespace kit {
- static const char _kit_thread_name[] = "kit_sdl2 thread";
- //returns whether or not priority of current thread was successfully set
- //may fail if setting priority requires elevated privileges (or at least when
- //setting a higher priority), if it's even allowed at all
- bool Thread_setPriority(s32 priority, bool throwOnFailure){
- SDL_ThreadPriority priority_real;
- switch(priority){
- case THREAD_LOW : priority_real = SDL_THREAD_PRIORITY_LOW; break;
- case THREAD_NORMAL : priority_real = SDL_THREAD_PRIORITY_NORMAL; break;
- case THREAD_HIGH : priority_real = SDL_THREAD_PRIORITY_HIGH; break;
- case THREAD_HIGHEST: priority_real = SDL_THREAD_PRIORITY_TIME_CRITICAL; break;
- default: THROW_ERROR("Thread_setPriority(): invalid thread priority");
- }
- bool success = !(SDL_SetThreadPriority(priority_real)<0);
- if(!success && throwOnFailure)
- THROW_ERROR("Thread_setPriority(): failed to set thread priority");
- return success;
- }
- struct _ThreadOpq { //24B (17 if you don't count the padding)
- ThreadFunction func;
- void* userdata;
- bool outsideCreate; //set after exiting SDL_CreateThreadWithStackSize()
- u8 _padding8;
- u16 _padding16;
- u32 _padding32;
- };
- static s32 ThreadFunctionWrapper(void* data){
- _ThreadOpq* _opq = (_ThreadOpq*)data;
- if(_opq == nullptr) return 0; //should be impossible
- while(!_opq->outsideCreate) SDL_Delay(1); //one of the solutions of all time
- _ThreadOpq opq = *_opq; //make stack copy
- memory::free(&_opq); //free original
- s32 result = 0;
- try {
- if(opq.func) result = opq.func(opq.userdata);
- } catch(const char* errortext){
- SDL_threadID thread_id = SDL_GetThreadID(nullptr);
- const char* thread_name = SDL_GetThreadName(nullptr);
- if(thread_name == nullptr) thread_name = "NAMELESS";
- kit_LogError("ON THREAD %lu (%s): %s",
- thread_id, thread_name, errortext);
- freeThreadErrors();
- }
- return result;
- }
- Thread::Thread(ThreadFunction func, void* userdata,
- bool waitInDestructor, size_t stackSize,
- const char* threadName)
- {
- if(_valid) return;
- _type = KIT_CLASSTYPE_THREAD;
- //handle parameters
- if(func == nullptr)
- THROW_ERROR("Thread::Thread(): nullptr was given as ThreadFunction");
- _waitInDestructor = waitInDestructor;
- if(threadName == nullptr) threadName = KIT_THREAD_NAME; //set to default if nullptr
- _ThreadOpq* opq = (_ThreadOpq*)memory::alloc(sizeof(_ThreadOpq));
- if(opq == nullptr)
- THROW_ERROR("Thread::Thread(): failed to allocate memory for Thread's opaque struct");
- memset(opq, 0, sizeof(_ThreadOpq));
- opq->func = func;
- opq->userdata = userdata;
- //(can't use THREAD_PTR on lvalue)
- _thread = (GenOpqPtr)SDL_CreateThreadWithStackSize(ThreadFunctionWrapper,
- threadName, stackSize, opq);
- opq->outsideCreate = true;
- if(THREAD_PTR == nullptr){
- memory::free(&opq);
- THROW_ERRORF("Thread::Thread(): \"%s\"", SDL_GetError());
- }
- _valid = true;
- _constructing = false;
- }
- Thread::~Thread(){
- if(!_valid) return;
- _valid = false;
- if(THREAD_PTR != nullptr){
- if(_waitInDestructor && !_detached)
- SDL_WaitThread(THREAD_PTR, nullptr); //cleans up memory too
- else if(!_detached)
- SDL_DetachThread(THREAD_PTR);
- }
- }
- //returns ID of current thread instead if thread is nullptr
- u32 Thread::getID(){
- if(KIT_IS_INVALID_THREAD)
- THROW_ERROR("Thread::getID(): " KIT_INVALID_THREAD);
- return SDL_GetThreadID((SDL_Thread*)_thread);
- }
- //returns UTF-8 string, or nullptr if thread doesn't have a name
- const char* Thread::getName(){
- if(KIT_IS_INVALID_THREAD)
- THROW_ERROR("Thread::getName(): " KIT_INVALID_THREAD);
- return SDL_GetThreadName(THREAD_PTR);
- }
- s32 Thread::waitUntilDone(){
- if(KIT_IS_INVALID_THREAD)
- THROW_ERROR("Thread::waitUntilDone(): " KIT_INVALID_THREAD);
- s32 returnStatus = 0;
- if(!_detached) SDL_WaitThread((SDL_Thread*)_thread, &returnStatus);
- return returnStatus;
- }
- void Thread::detach(){
- if(KIT_IS_INVALID_THREAD)
- THROW_ERROR("Thread::detach(): " KIT_INVALID_THREAD);
- if(!_detached){
- SDL_DetachThread(THREAD_PTR);
- _detached = true;
- }
- }
- }; /* namespace kit */
- /******************************************************************************/
- /******************************************************************************/
- //"2024-11-21\src\kit_sdl2\kit_time.cpp":
- #include "_kit_common.hpp"
- namespace kit {
- timerID timerAdd(timerCallback func, u32 interval_ms, void* userdata){
- if(!_gl.init.timer) THROW_ERROR("timerAdd(): timer subsystem is uninitialized");
- timerID id_new = SDL_AddTimer(interval_ms, func, userdata);
- if(!id_new) THROW_ERRORF("timerAdd(): \"%s\"", SDL_GetError())//(";" can't be used here)
- else return id_new;
- }
- void timerRemove(timerID id){
- if(!_gl.init.timer) THROW_ERROR("timerRemove(): timer subsystem is uninitialized");
- bool success = SDL_RemoveTimer(id);
- if(!success) THROW_ERRORF("timerRemove(): \"%s\"", SDL_GetError());
- }
- u64 time::getTicks(){
- return SDL_GetPerformanceCounter();
- }
- u64 time::getTicksPerSecond(){
- if(!_gl.performanceFreq)
- _gl.performanceFreq = SDL_GetPerformanceFrequency();
- return _gl.performanceFreq;
- }
- u64 time::getMS(){
- return SDL_GetTicks64();
- }
- u32 time::getMS_32(){
- return SDL_GetTicks();
- }
- f64 time::getSeconds(){
- return (f64)SDL_GetTicks64()/1000;
- }
- void time::sleep(u32 milliseconds){
- SDL_Delay(milliseconds);
- }
- }; /* namespace kit */
- /******************************************************************************/
- /******************************************************************************/
- //"2024-11-21\src\kit_sdl2\kit_video_func.cpp":
- #include "_kit_common.hpp"
- namespace kit {
- //len of 21, assumming "PIXELFMT_ARGB2101010\x00" is the longest format name
- static char _pixelFmtNames[39][21];
- static bool _pixelFmtNamesInit[39];
- const char* getPixelFmtName(u32 pixel_format){
- u32 which = 0;
- switch(pixel_format){
- case PIXELFMT_INDEX1LSB : which = 1; break;
- case PIXELFMT_INDEX1MSB : which = 2; break;
- case PIXELFMT_INDEX4LSB : which = 3; break;
- case PIXELFMT_INDEX4MSB : which = 4; break;
- case PIXELFMT_INDEX8 : which = 5; break;
- case PIXELFMT_RGB332 : which = 6; break;
- case PIXELFMT_RGB444 : which = 7; break;
- case PIXELFMT_BGR444 : which = 8; break;
- case PIXELFMT_RGB555 : which = 9; break;
- case PIXELFMT_BGR555 : which = 10; break;
- case PIXELFMT_ARGB4444 : which = 11; break;
- case PIXELFMT_RGBA4444 : which = 12; break;
- case PIXELFMT_ABGR4444 : which = 13; break;
- case PIXELFMT_BGRA4444 : which = 14; break;
- case PIXELFMT_ARGB1555 : which = 15; break;
- case PIXELFMT_RGBA5551 : which = 16; break;
- case PIXELFMT_ABGR1555 : which = 17; break;
- case PIXELFMT_BGRA5551 : which = 18; break;
- case PIXELFMT_RGB565 : which = 19; break;
- case PIXELFMT_BGR565 : which = 20; break;
- case PIXELFMT_RGB24 : which = 21; break;
- case PIXELFMT_BGR24 : which = 22; break;
- case PIXELFMT_RGB888 : which = 23; break;
- case PIXELFMT_RGBX8888 : which = 24; break;
- case PIXELFMT_BGR888 : which = 25; break;
- case PIXELFMT_BGRX8888 : which = 26; break;
- case PIXELFMT_ARGB8888 : which = 27; break;
- case PIXELFMT_RGBA8888 : which = 28; break;
- case PIXELFMT_ABGR8888 : which = 29; break;
- case PIXELFMT_BGRA8888 : which = 30; break;
- case PIXELFMT_ARGB2101010: which = 31; break;
- case PIXELFMT_YV12 : which = 32; break;
- case PIXELFMT_IYUV : which = 33; break;
- case PIXELFMT_YUY2 : which = 34; break;
- case PIXELFMT_UYVY : which = 35; break;
- case PIXELFMT_YVYU : which = 36; break;
- case PIXELFMT_NV12 : which = 37; break;
- case PIXELFMT_NV21 : which = 38; break;
- default:;
- }
- char* name_kit = _pixelFmtNames[which];
- if(!_pixelFmtNamesInit[which]){
- const char* name_sdl = SDL_GetPixelFormatName(pixel_format);
- name_sdl += sizeof("SDL_PIXELFORMAT")-1; //skip the "SDL_PIXELFORMAT" part
- strnCpy(name_kit , "PIXELFMT", 8);
- strnCpy(name_kit+8, name_sdl, 12);
- name_kit[20] = 0; //manual null-terminator, just in case
- _pixelFmtNamesInit[which] = true;
- }
- return (const char*)name_kit;
- }
- }; /* namespace kit */
- /******************************************************************************/
- /******************************************************************************/
- //"2024-11-21\src\kit_sdl2\kit_Window.cpp":
- #include "_kit_common.hpp"
- #define WIN_PTR ((SDL_Window*)_win)
- #define SURF_PTR ((SDL_Surface*)_surf)
- #define KIT_WIN_IS_INVALID (!_valid)
- #define KIT_WIN_CHECK_INVALID(_funcname) \
- if(KIT_WIN_IS_INVALID) THROW_ERROR(_funcname ": window is invalid");
- #define WIN_DATA_NAME "Window class"
- namespace kit {
- /* functions to potentially maybe incorporate:
- SDL_GetWindowBordersSize
- SDL_GetWindowGammaRamp
- SDL_SetWindowGammaRamp
- SDL_SetWindowHitTest
- SDL_SetWindowShape
- SDL_IsShapedWindow
- SDL_SetWindowModalFor
- */
- #define VALID_WINFLAGS ( \
- WINFLAG_FULLSCREEN | \
- WINFLAG_OPENGL | \
- WINFLAG_HIDDEN | \
- WINFLAG_BORDERLESS | \
- WINFLAG_RESIZABLE | \
- WINFLAG_MINIMIZED | \
- WINFLAG_MAXIMIZED | \
- WINFLAG_INPUT_GRABBED | \
- WINFLAG_FULLSCREEN_DESKTOP | \
- WINFLAG_VULKAN \
- )
- Window::Window(const char* winTitle,
- u31 winWidth, u31 winHeight,
- u32 winFlags,
- s32 winX, s32 winY)
- {
- if(_valid) return;
- _type = KIT_CLASSTYPE_WINDOW;
- if(winWidth.s ) THROW_ERROR("Window::Window(): winWidth > KIT_S32_MAX");
- if(winHeight.s) THROW_ERROR("Window::Window(): winHeight > KIT_S32_MAX");
- _win = SDL_CreateWindow(winTitle, winX, winY,
- winWidth.n, winHeight.n,
- winFlags & VALID_WINFLAGS);
- if(_win == nullptr)
- THROW_ERRORF("Window::Window(): \"%s\"", SDL_GetError());
- SDL_SetWindowData(WIN_PTR, WIN_DATA_NAME, this);
- _valid = true;
- _constructing = false;
- }
- Window::~Window(){
- if(!_valid) return;
- _valid = false;
- if(_win != nullptr){
- SDL_DestroyWindow(WIN_PTR);
- _win = nullptr;
- }
- }
- bool Window::hasSurface(){
- KIT_WIN_CHECK_INVALID("Window::hasSurface()");
- return SDL_HasWindowSurface(WIN_PTR);
- }
- u32 Window::getPixelFormat(){
- KIT_WIN_CHECK_INVALID("Window::getPixelFormat()");
- u32 pixelFormat = SDL_GetWindowPixelFormat(WIN_PTR);
- if(pixelFormat == SDL_PIXELFORMAT_UNKNOWN)
- THROW_ERRORF("Window::getPixelFormat(): \"%s\"", SDL_GetError());
- return pixelFormat;
- }
- u32 Window::getID(){
- KIT_WIN_CHECK_INVALID("Window::getID()");
- u32 id = SDL_GetWindowID(WIN_PTR);
- if(!id) THROW_ERRORF("Window::getID(): \"%s\"", SDL_GetError());
- return id;
- }
- u32 Window::getDisplayIndex(){
- KIT_WIN_CHECK_INVALID("Window::getDisplayIndex()");
- s32 index = SDL_GetWindowDisplayIndex(WIN_PTR);
- if(index < 0)
- THROW_ERRORF("Window::getDisplayIndex(): \"%s\"", SDL_GetError());
- return (u32)index;
- }
- void* Window::getUserdata(const char* name){
- KIT_WIN_CHECK_INVALID("Window::getUserdata()");
- if(name == nullptr)
- THROW_ERROR("Window::getUserdata(): name = nullptr");
- return SDL_GetWindowData(WIN_PTR, name);
- }
- DisplayMode Window::getDisplayMode(){
- KIT_WIN_CHECK_INVALID("Window::getDisplayMode()");
- SDL_DisplayMode mode_sdl;
- if(SDL_GetWindowDisplayMode(WIN_PTR, &mode_sdl) < 0)
- THROW_ERRORF("Window::getDisplayMode(): \"%s\"", SDL_GetError());
- DisplayMode mode_kit;
- mode_kit.pixelFmt = mode_sdl.format;
- mode_kit.w = mode_sdl.w;
- mode_kit.h = mode_sdl.h;
- mode_kit.refreshRate = mode_sdl.refresh_rate;
- return mode_kit;
- }
- const char* Window::getTitle(){
- KIT_WIN_CHECK_INVALID("Window::getTitle()");
- return SDL_GetWindowTitle(WIN_PTR);
- }
- shape::point Window::getSize(){
- KIT_WIN_CHECK_INVALID("Window::getSize()");
- shape::point size;
- SDL_GetWindowSize(WIN_PTR, &size.x, &size.y);
- return size;
- }
- u32 Window::getFlags(){
- KIT_WIN_CHECK_INVALID("Window::getFlags()");
- return SDL_GetWindowFlags(WIN_PTR);
- }
- f32 Window::getOpacity(){
- KIT_WIN_CHECK_INVALID("Window::getOpacity()");
- f32 opacity;
- if(SDL_GetWindowOpacity(WIN_PTR, &opacity) < 0)
- THROW_ERRORF("Window::getOpacity(): \"%s\"", SDL_GetError());
- return opacity;
- }
- shape::point Window::getMinSize(){
- KIT_WIN_CHECK_INVALID("Window::getMinSize()");
- shape::point minSize;
- SDL_GetWindowMinimumSize(WIN_PTR, &minSize.x, &minSize.y);
- return minSize;
- }
- shape::point Window::getMaxSize(){
- KIT_WIN_CHECK_INVALID("Window::getMaxSize()");
- shape::point maxSize;
- SDL_GetWindowMaximumSize(WIN_PTR, &maxSize.x, &maxSize.y);
- return maxSize;
- }
- shape::point Window::getPosition(){
- KIT_WIN_CHECK_INVALID("Window::getPosition()");
- shape::point pos;
- SDL_GetWindowPosition(WIN_PTR, &pos.x, &pos.y);
- return pos;
- }
- bool Window::getGrab(){
- KIT_WIN_CHECK_INVALID("Window::getGrab()");
- return SDL_GetWindowGrab(WIN_PTR);
- }
- bool Window::getKeyboardGrab(){
- KIT_WIN_CHECK_INVALID("Window::getKeyboardGrab()");
- return SDL_GetWindowKeyboardGrab(WIN_PTR);
- }
- bool Window::getMouseGrab(){
- KIT_WIN_CHECK_INVALID("Window::getMouseGrab()");
- return SDL_GetWindowMouseGrab(WIN_PTR);
- }
- shape::rect Window::getMouseGrabRect(){
- KIT_WIN_CHECK_INVALID("Window::getMouseGrabRect()");
- const SDL_Rect* _rect = SDL_GetWindowMouseRect(WIN_PTR);
- shape::rect rect;
- if(_rect != nullptr){
- rect.x = _rect->x;
- rect.y = _rect->y;
- rect.w = _rect->w;
- rect.h = _rect->h;
- } else { //no rect set; use full window
- shape::point winSize = getSize();
- rect.x = 0;
- rect.y = 0;
- rect.w = winSize.x;
- rect.h = winSize.y;
- }
- return rect;
- }
- f32 Window::getBrightness(){
- KIT_WIN_CHECK_INVALID("Window::getBrightness()");
- return SDL_GetWindowBrightness(WIN_PTR);
- }
- void* Window::setUserdata(const char* name, void* userdata){
- KIT_WIN_CHECK_INVALID("Window::setUserdata()");
- if(name == nullptr)
- THROW_ERROR("Window::setUserdata(): name = nullptr");
- if(!strnCmp(WIN_DATA_NAME, name)) //WIN_DATA_NAME is reserved
- THROW_ERROR("Window::setUserdata(): name = \"" WIN_DATA_NAME "\"");
- return SDL_SetWindowData(WIN_PTR, name, userdata);
- }
- void Window::setDisplayMode(const DisplayMode* mode){
- KIT_WIN_CHECK_INVALID("Window::setDisplayMode()");
- SDL_DisplayMode mode_sdl;
- if(mode != nullptr){
- mode_sdl.format = mode->pixelFmt;
- mode_sdl.w = mode->w;
- mode_sdl.h = mode->h;
- mode_sdl.refresh_rate = mode->refreshRate;
- }
- if(SDL_SetWindowDisplayMode(WIN_PTR, (mode) ? &mode_sdl : nullptr) < 0)
- THROW_ERRORF("Window::setDisplayMode(): \"%s\"", SDL_GetError());
- }
- void Window::setTitle(const char* title){
- KIT_WIN_CHECK_INVALID("Window::setTitle()");
- if(title == nullptr) title = "";
- SDL_SetWindowTitle(WIN_PTR, title);
- }
- void Window::setSize(u31 width, u31 height){
- KIT_WIN_CHECK_INVALID("Window::setSize()");
- if(width.s ) THROW_ERROR("Window::setSize(): width > KIT_S32_MAX");
- if(height.s) THROW_ERROR("Window::setSize(): height > KIT_S32_MAX");
- SDL_SetWindowSize(WIN_PTR, width.n, height.n);
- }
- void Window::setVisibility(bool visible){
- KIT_WIN_CHECK_INVALID("Window::setVisibility()");
- if(visible) SDL_ShowWindow(WIN_PTR);
- else SDL_HideWindow(WIN_PTR);
- }
- void Window::setFullscreen(u32 mode){
- KIT_WIN_CHECK_INVALID("Window::setFullscreen()");
- u32 flags = 0; //normal windowed mode by default
- if (mode == 1) flags = SDL_WINDOW_FULLSCREEN;
- else if(mode == 2) flags = SDL_WINDOW_FULLSCREEN_DESKTOP;
- if(SDL_SetWindowFullscreen(WIN_PTR, flags) < 0)
- THROW_ERRORF("Window::setFullscreen(): \"%s\"", SDL_GetError());
- }
- void Window::setResizable(bool enable){
- KIT_WIN_CHECK_INVALID("Window::setResizable()");
- SDL_SetWindowResizable(WIN_PTR, (SDL_bool)enable);
- }
- void Window::setBordered(bool enable){
- KIT_WIN_CHECK_INVALID("Window::setBordered()");
- SDL_SetWindowBordered(WIN_PTR, (SDL_bool)enable);
- }
- void Window::setAlwaysOnTop(bool enable){
- KIT_WIN_CHECK_INVALID("Window::setAlwaysOnTop()");
- SDL_SetWindowAlwaysOnTop(WIN_PTR, (SDL_bool)enable);
- }
- void Window::setOpacity(f32 opacity){
- KIT_WIN_CHECK_INVALID("Window::setOpacity()");
- if(SDL_SetWindowOpacity(WIN_PTR, opacity) < 0)
- THROW_ERRORF("Window::setOpacity(): \"%s\"", SDL_GetError());
- }
- void Window::setMinSize(u31 minWidth, u31 minHeight){
- KIT_WIN_CHECK_INVALID("Window::setMinSize()");
- if(minWidth.s ) THROW_ERROR("Window::setMinSize(): minWidth > KIT_S32_MAX");
- if(minHeight.s) THROW_ERROR("Window::setMinSize(): minHeight > KIT_S32_MAX");
- SDL_SetWindowMinimumSize(WIN_PTR, minWidth.n, minHeight.n);
- }
- void Window::setMaxSize(u31 maxWidth, u31 maxHeight){
- KIT_WIN_CHECK_INVALID("Window::setMaxSize()");
- if(maxWidth.s ) THROW_ERROR("Window::setMaxSize(): maxWidth > KIT_S32_MAX");
- if(maxHeight.s) THROW_ERROR("Window::setMaxSize(): maxHeight > KIT_S32_MAX");
- SDL_SetWindowMaximumSize(WIN_PTR, maxWidth.n, maxHeight.n);
- }
- void Window::setPosition(s32 x, s32 y){
- KIT_WIN_CHECK_INVALID("Window::setPosition()");
- SDL_SetWindowPosition(WIN_PTR, x, y);
- }
- void Window::setGrab(bool enable){
- KIT_WIN_CHECK_INVALID("Window::setGrab()");
- SDL_SetWindowGrab(WIN_PTR, (SDL_bool)enable);
- }
- void Window::setKeyboardGrab(bool enable){
- KIT_WIN_CHECK_INVALID("Window::setKeyboardGrab()");
- SDL_SetWindowKeyboardGrab(WIN_PTR, (SDL_bool)enable);
- }
- void Window::setMouseGrab(bool enable){
- KIT_WIN_CHECK_INVALID("Window::setMouseGrab()");
- SDL_SetWindowMouseGrab(WIN_PTR, (SDL_bool)enable);
- }
- void Window::setMouseGrabRect(const shape::rect* rect){
- KIT_WIN_CHECK_INVALID("Window::setMouseGrabRect()");
- if(SDL_SetWindowMouseRect(WIN_PTR, (const SDL_Rect*)rect) < 0)
- THROW_ERRORF("Window::setMouseGrabRect(): \"%s\"", SDL_GetError());
- }
- void Window::setBrightness(f32 brightness){
- KIT_WIN_CHECK_INVALID("Window::setBrightness()");
- if(SDL_SetWindowBrightness(WIN_PTR, brightness) < 0)
- THROW_ERRORF("Window::setBrightness(): \"%s\"", SDL_GetError());
- }
- void Window::setIcon(Surface& icon){
- KIT_WIN_CHECK_INVALID("Window::setIcon()");
- void* icon_p = &icon;
- if(!KIT_GET_CLASS_VALID(icon_p))
- THROW_ERROR("Window::setIcon(): icon is invalid");
- SDL_Surface* icon_surf = (SDL_Surface*)KIT_GET_CLASS_OPAQUE(icon_p);
- SDL_SetWindowIcon(WIN_PTR, icon_surf);
- }
- void Window::warpMouse(s32 x, s32 y){
- KIT_WIN_CHECK_INVALID("Window::warpMouse()");
- SDL_WarpMouseInWindow(WIN_PTR, x, y);
- }
- void Window::minimize(){
- KIT_WIN_CHECK_INVALID("Window::minimize()");
- SDL_MinimizeWindow(WIN_PTR);
- }
- void Window::maximize(){
- KIT_WIN_CHECK_INVALID("Window::maximize()");
- SDL_MaximizeWindow(WIN_PTR);
- }
- void Window::restore(){
- KIT_WIN_CHECK_INVALID("Window::restore()");
- SDL_RestoreWindow(WIN_PTR);
- }
- void Window::raise(){
- KIT_WIN_CHECK_INVALID("Window::raise()");
- SDL_RaiseWindow(WIN_PTR);
- }
- /******************************************************************************/
- bool Window::renewSurface(){
- if(KIT_WIN_IS_INVALID) return false;
- _surf = SDL_GetWindowSurface(WIN_PTR);
- if(_surf == nullptr)
- THROW_ERRORF("Window::renewSurface(): \"%s\"", SDL_GetError());
- return true;
- }
- void Window::destroySurface(){
- KIT_WIN_CHECK_INVALID("Window::destroySurface()");
- if(_surf == nullptr) return;
- if(SDL_DestroyWindowSurface(WIN_PTR) < 0)
- THROW_ERRORF("Window::destroySurface(): \"%s\"", SDL_GetError());
- _surf = nullptr;
- }
- void Window::updateSurface(const shape::rect* rects, u31 rects_len){
- KIT_WIN_CHECK_INVALID("Window::updateSurface()");
- if(rects_len.s)
- THROW_ERROR("Window::updateSurface(): rects_len > KIT_S32_MAX");
- int err;
- if(rects == nullptr) err = SDL_UpdateWindowSurface(WIN_PTR);
- else err = SDL_UpdateWindowSurfaceRects(WIN_PTR, (SDL_Rect*)rects, rects_len.n);
- if(err < 0)
- THROW_ERRORF("Window::updateSurface(): \"%s\"", SDL_GetError());
- }
- void Window::fillRects(colors::ABGR color, const shape::rect* rects,
- u31 rects_len)
- {
- KIT_WIN_CHECK_INVALID("Window::fillRects()");
- if(_surf == nullptr)
- THROW_ERROR("Window::fillRects(): Window doesn't have a surface");
- if(rects_len.s)
- THROW_ERROR("Window::fillRects(): rects_len > KIT_S32_MAX");
- u32 mapped_color = SDL_MapRGBA(SURF_PTR->format, color.r, color.g,
- color.b, color.a);
- int err;
- if(rects == nullptr || !rects_len.n){
- err = SDL_FillRect(SURF_PTR, (SDL_Rect*)rects, mapped_color);
- } else {
- err = SDL_FillRects(SURF_PTR, (SDL_Rect*)rects, rects_len.n, mapped_color);
- }
- if(err < 0)
- THROW_ERRORF("Window::fillRects(): \"%s\"", SDL_GetError());
- }
- /******************************************************************************/
- Window* getGrabbedWindow(){ //not a part of Window
- SDL_Window* win_sdl = SDL_GetGrabbedWindow();
- if(win_sdl == nullptr) return nullptr;
- return (Window*)SDL_GetWindowData(win_sdl, WIN_DATA_NAME);
- }
- }; /* namespace kit */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement