142 lines
4.7 KiB
C
142 lines
4.7 KiB
C
|
|
/* - | Copyright | --------------------------------------------------------------
|
||
|
|
Copyright (c) 2026 Randy Jordan
|
||
|
|
|
||
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||
|
|
this software and associated documentation files (the "Software"), to deal in
|
||
|
|
the Software without restriction, including without limitation the rights to
|
||
|
|
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||
|
|
of the Software, and to permit persons to whom the Software is furnished to do
|
||
|
|
so, subject to the following conditions:
|
||
|
|
|
||
|
|
The above copyright notice and this permission notice shall be included in all
|
||
|
|
copies or substantial portions of the Software.
|
||
|
|
|
||
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||
|
|
SOFTWARE.
|
||
|
|
|
||
|
|
* --------------------------------------------------------------------------*/
|
||
|
|
#define DEBUG
|
||
|
|
#define GUI_DEBUG_CLOSE
|
||
|
|
|
||
|
|
#include "../include/ci2.h"
|
||
|
|
#include <stdlib.h>
|
||
|
|
#include <string.h>
|
||
|
|
|
||
|
|
static int test_caught_exception(){
|
||
|
|
size_t nbytes = CI2_GB(1024);
|
||
|
|
void *buf = NULL;
|
||
|
|
const char *str = "Hello World";
|
||
|
|
buf = CI2_CALLOC(1,strlen(str+1)); /* You have no enable asan options. */
|
||
|
|
strncpy(buf,str,strlen(str));
|
||
|
|
|
||
|
|
char *opts = "ASAN_OPTIONS=allocator_may_fail=1";
|
||
|
|
|
||
|
|
printf("Attempting oom re-allocation of %ld at %p\n", nbytes, buf);
|
||
|
|
printf("Adding %s to env....%d\n",opts, putenv(opts));
|
||
|
|
|
||
|
|
CI2_TRY {
|
||
|
|
printf("Trying something dumb.\n");
|
||
|
|
CI2_RESIZE(buf, nbytes);
|
||
|
|
}
|
||
|
|
CI2_EXCEPT(oom) {
|
||
|
|
/* handle memory failure gracefully */
|
||
|
|
printf("Caught: %d: %s\n", oom.num, oom.msg);
|
||
|
|
return EXIT_SUCCESS; /* requested behavior */
|
||
|
|
}
|
||
|
|
CI2_FINALLY {
|
||
|
|
/* cleanup if needed, runs always */
|
||
|
|
printf("Have to free allocation of %ld at %p\n",strlen(buf), buf );
|
||
|
|
if (buf) free(buf);
|
||
|
|
}
|
||
|
|
CI2_END_TRY;
|
||
|
|
return EXIT_FAILURE;
|
||
|
|
}
|
||
|
|
static int test_uncaught_exception(void){
|
||
|
|
#ifdef CI2_EXCEPTION_CRASH
|
||
|
|
printf("CI2_MSG — Deliberate Failure (expect a debug break)\n");
|
||
|
|
printf(" Triggering ci2_raise_exception() now...\n");
|
||
|
|
fflush(stdout);
|
||
|
|
|
||
|
|
struct CI2_Exception u = { EFAULT, "Deliberate uncaught exception test."};
|
||
|
|
CI2_RAISE(u);
|
||
|
|
printf(" [FAIL] Execution continued past a failing ASSERT_MSG!\n");
|
||
|
|
return EXIT_SUCCESS; // Shouldn't but still.
|
||
|
|
#endif
|
||
|
|
return EXIT_SUCCESS;
|
||
|
|
}
|
||
|
|
|
||
|
|
#ifdef CI2_WINDOWS
|
||
|
|
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
|
||
|
|
{
|
||
|
|
(void)hInstance;
|
||
|
|
(void)hPrevInstance;
|
||
|
|
(void)lpCmdLine;
|
||
|
|
(void) nShowCmd;
|
||
|
|
if (!ci2_init()) {
|
||
|
|
MessageBoxA(NULL, "ci2_init failed", "Error", MB_OK | MB_ICONERROR);
|
||
|
|
return EXIT_FAILURE;
|
||
|
|
}
|
||
|
|
static ci2_sys_char *ci2_window_title = L"Win32 CI2 Window Title";
|
||
|
|
CI2_Window* window = ci2_create_window(ci2_window_title, 800, 600);
|
||
|
|
if (!window) {
|
||
|
|
MessageBoxA(NULL, "ci2_create_window failed", "Error", MB_OK | MB_ICONERROR);
|
||
|
|
ci2_shutdown();
|
||
|
|
return EXIT_FAILURE;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 4) Main loop: run until ci2_poll_events reports the window should close.
|
||
|
|
// This loop is friendly to rendering or other per-frame work.
|
||
|
|
int result = -1;
|
||
|
|
while (ci2_poll_events(window)) {
|
||
|
|
// Per-frame update/render would go here.
|
||
|
|
// Example: simple sleep to avoid busy-looping; adjust to suit your app's timing.
|
||
|
|
|
||
|
|
result = test_uncaught_exception();
|
||
|
|
result = test_caught_exception();
|
||
|
|
Sleep(1);
|
||
|
|
#ifdef GUI_DEBUG_CLOSE
|
||
|
|
break;
|
||
|
|
#endif
|
||
|
|
}
|
||
|
|
|
||
|
|
// 5) Destroy window and shutdown ci2.
|
||
|
|
ci2_destroy_window(window);
|
||
|
|
ci2_shutdown();
|
||
|
|
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
#else /* non-Windows (Linux, macOS, etc.) */
|
||
|
|
#include <unistd.h>
|
||
|
|
int main(int argc, ci2_sys_char *argv[])
|
||
|
|
{
|
||
|
|
UNUSED(argc);
|
||
|
|
UNUSED(argv);
|
||
|
|
if (!ci2_init()) return 1;
|
||
|
|
|
||
|
|
static ci2_sys_char *ci2_window_title = "Linux CI2 Window Title";
|
||
|
|
CI2_Window* window = ci2_create_window(ci2_window_title, 800, 600);
|
||
|
|
if (!window) return 1;
|
||
|
|
|
||
|
|
int result = -1;
|
||
|
|
while (ci2_poll_events(window)) {
|
||
|
|
// Your rendering / game loop goes here
|
||
|
|
|
||
|
|
result = test_uncaught_exception();
|
||
|
|
result = test_caught_exception();
|
||
|
|
sleep(1);
|
||
|
|
#ifdef GUI_DEBUG_CLOSE
|
||
|
|
break;
|
||
|
|
#endif
|
||
|
|
}
|
||
|
|
|
||
|
|
ci2_destroy_window(window);
|
||
|
|
ci2_shutdown();
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
#endif
|