Initial commit
This commit is contained in:
21
LICENSE
Normal file
21
LICENSE
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) [year] [fullname]
|
||||||
|
|
||||||
|
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.
|
||||||
110
Makefile
Normal file
110
Makefile
Normal file
@@ -0,0 +1,110 @@
|
|||||||
|
# Compiler Flags
|
||||||
|
CC := gcc
|
||||||
|
CFLAGS := -g -Wall -Wextra -Werror -pedantic -fno-omit-frame-pointer
|
||||||
|
|
||||||
|
# Directory Variables
|
||||||
|
LIBDIR := lib
|
||||||
|
OBJ := obj
|
||||||
|
INC := include
|
||||||
|
SRC := src
|
||||||
|
TEST := tests
|
||||||
|
|
||||||
|
# Platform Detection and Library Linking
|
||||||
|
ifeq ($(OS), Windows_NT)
|
||||||
|
PLATFORM := win32
|
||||||
|
PLATFORM_SRC := $(SRC)/windows_platform.c
|
||||||
|
PLATFORM_OBJ := $(OBJ)/windows_platform.o
|
||||||
|
PLATFORM_FLAGS := -D WIN32
|
||||||
|
PLATFORM_LIBS := -mwindows
|
||||||
|
else
|
||||||
|
UNAME_S := $(shell uname -s)
|
||||||
|
ifeq ($(UNAME_S), Linux)
|
||||||
|
PLATFORM := linux
|
||||||
|
PLATFORM_SRC := $(SRC)/linux_platform.c
|
||||||
|
PLATFORM_OBJ := $(OBJ)/linux_platform.o
|
||||||
|
PLATFORM_FLAGS := -D LINUX
|
||||||
|
PLATFORM_LIBS := -lX11
|
||||||
|
else ifeq ($(UNAME_S), Darwin)
|
||||||
|
PLATFORM := macos
|
||||||
|
PLATFORM_SRC := $(SRC)/macos_platform.c
|
||||||
|
PLATFORM_OBJ := $(OBJ)/macos_platform.o
|
||||||
|
PLATFORM_FLAGS := -D MACOS
|
||||||
|
PLATFORM_LIBS :=
|
||||||
|
else
|
||||||
|
$(error Unsupported platform: $(UNAME_S))
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
|
||||||
|
|
||||||
|
CFLAGS += $(PLATFORM_FLAGS)
|
||||||
|
# Filepath Pattern Matching
|
||||||
|
LIB := $(LIBDIR)/lib.a
|
||||||
|
# Exclude platform files from wildcard, add the correct one back in
|
||||||
|
SRCS := $(filter-out $(SRC)/windows_platform.c $(SRC)/linux_platform.c $(SRC)/macos_platform.c, \
|
||||||
|
$(wildcard $(SRC)/*.c)) $(PLATFORM_SRC)
|
||||||
|
OBJS := $(patsubst $(SRC)/%.c, $(OBJ)/%.o, $(SRCS))
|
||||||
|
TESTS := $(wildcard $(TEST)/*.c)
|
||||||
|
TESTBINS := $(patsubst $(TEST)/%.c, $(TEST)/bin/%, $(TESTS))
|
||||||
|
|
||||||
|
|
||||||
|
# Commands must be labeled PHONY
|
||||||
|
.PHONY: all release clean test
|
||||||
|
# Compiler Release Flags
|
||||||
|
release: CFLAGS := -Wall -Wextra -Werror -pedantic -fsanitize=address,undefined -fno-omit-frame-pointer -O2 -DNDEBUG $(PLATFORM_FLAGS)
|
||||||
|
release: clean $(LIB)
|
||||||
|
|
||||||
|
|
||||||
|
# Target for compilation.
|
||||||
|
all: $(LIB)
|
||||||
|
@echo "Built for platform: $(PLATFORM)"
|
||||||
|
# Target / Dependencies
|
||||||
|
$(LIB): $(OBJS) | $(LIBDIR)
|
||||||
|
$(RM) $(LIB)
|
||||||
|
ar -cvrs $@ $^
|
||||||
|
$(OBJ)/%.o: $(SRC)/%.c $(SRC)/%.h | $(OBJ)
|
||||||
|
$(CC) $(CFLAGS) -c $< -o $@
|
||||||
|
$(OBJ)/%.o: $(SRC)/%.c | $(OBJ)
|
||||||
|
$(CC) $(CFLAGS) -c $< -o $@
|
||||||
|
$(TEST)/bin/%: $(TEST)/%.c $(LIB) | $(TEST)/bin
|
||||||
|
$(CC) $(CFLAGS) $< $(LIB) $(PLATFORM_LIBS) -o $@
|
||||||
|
|
||||||
|
# Make directories if none.
|
||||||
|
$(LIBDIR):
|
||||||
|
mkdir $@
|
||||||
|
$(INC):
|
||||||
|
mkdir $@
|
||||||
|
$(OBJ):
|
||||||
|
mkdir $@
|
||||||
|
$(TEST)/bin:
|
||||||
|
mkdir $@
|
||||||
|
|
||||||
|
# Run the tests in the bin folder and track results
|
||||||
|
test: $(LIB) $(TESTBINS)
|
||||||
|
@SUCCESS=0; FAILURE=0; \
|
||||||
|
RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; NC='\033[0m'; \
|
||||||
|
for t in $(TESTBINS); do \
|
||||||
|
NAME=$$(basename $$t); \
|
||||||
|
START=$$(date +%s%N); \
|
||||||
|
if $$t; then \
|
||||||
|
RET=0; \
|
||||||
|
else \
|
||||||
|
RET=$$?; \
|
||||||
|
fi; \
|
||||||
|
END=$$(date +%s%N); \
|
||||||
|
ELAPSED_NS=$$((END - START)); \
|
||||||
|
ELAPSED_MS=$$((ELAPSED_NS / 1000000)); \
|
||||||
|
if [ $$RET -eq 0 ]; then \
|
||||||
|
printf "%-20s %bPASS%b (%b%4d ms%b)\n" "$$NAME" "$$GREEN" "$$NC" "$$YELLOW" "$$ELAPSED_MS" "$$NC"; \
|
||||||
|
SUCCESS=$$((SUCCESS + 1)); \
|
||||||
|
else \
|
||||||
|
printf "%-20s %bFAIL%b (%b%4d ms%b)\n" "$$NAME" "$$RED" "$$NC" "$$YELLOW" "$$ELAPSED_MS" "$$NC"; \
|
||||||
|
FAILURE=$$((FAILURE + 1)); \
|
||||||
|
fi; \
|
||||||
|
done; \
|
||||||
|
printf "\nTests completed\n"; \
|
||||||
|
printf "SUCCESS: %b%d%b\n" "$$GREEN" "$$SUCCESS" "$$NC"; \
|
||||||
|
printf "FAILURE: %b%d%b\n" "$$RED" "$$FAILURE" "$$NC"; \
|
||||||
|
test $$FAILURE -eq 0
|
||||||
|
|
||||||
|
clean:
|
||||||
|
$(RM) -r $(LIBDIR) $(OBJ) $(TEST)/bin/
|
||||||
29
README.md
Normal file
29
README.md
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
# platform
|
||||||
|
|
||||||
|
## Description
|
||||||
|
Basic example of "platform layer".
|
||||||
|
|
||||||
|
## Table of Contents
|
||||||
|
|
||||||
|
- [Description](#description)
|
||||||
|
- [Features](#features)
|
||||||
|
- [Usage](#usage)
|
||||||
|
- [Credits / Resources](#credits--resources)
|
||||||
|
- [License](#license)
|
||||||
|
|
||||||
|
## Features
|
||||||
|
- [x] Win32 Window
|
||||||
|
- [x] Linux X11 Window
|
||||||
|
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
## Credits / Resources
|
||||||
|
[Tom Preston-Werner README Driven Development](https://tom.preston-werner.com/2010/08/23/readme-driven-development)<br>
|
||||||
|
[Make a README](https://www.makeareadme.com/)<br>
|
||||||
|
[Choose a LICENSE](https://choosealicense.com/)<br>
|
||||||
|
[Handmade Hero](https://www.youtube.com/playlist?list=PLnuhp3Xd9PYTt6svyQPyRO_AAuMWGxPzU)<br>
|
||||||
|
|
||||||
|
|
||||||
|
## License
|
||||||
|
This project is licensed under MIT - see the [LICENSE](LICENSE) file for details.
|
||||||
107
include/except.h
Normal file
107
include/except.h
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
#ifndef EXCEPT_INCLUDED
|
||||||
|
#define EXCEPT_INCLUDED
|
||||||
|
|
||||||
|
#include <setjmp.h>
|
||||||
|
struct Exception {
|
||||||
|
const char *reason;
|
||||||
|
};
|
||||||
|
typedef struct Exception Exception;
|
||||||
|
|
||||||
|
struct Except_Frame {
|
||||||
|
struct Except_Frame *prev;
|
||||||
|
jmp_buf env;
|
||||||
|
const char *file;
|
||||||
|
int line;
|
||||||
|
const struct Exception *exception;
|
||||||
|
};
|
||||||
|
typedef struct Except_Frame Except_Frame;
|
||||||
|
|
||||||
|
enum { EXCEPT_STATE_ENTERED=0,
|
||||||
|
EXCEPT_STATE_RAISED,
|
||||||
|
EXCEPT_STATE_HANDLED,
|
||||||
|
EXCEPT_STATE_FINALIZED,
|
||||||
|
EXCEPT_STATE_COUNT};
|
||||||
|
|
||||||
|
extern struct Except_Frame *except_stack;
|
||||||
|
extern const struct Exception assertion_failed;
|
||||||
|
void except_raise(const Exception *e, const char *file, int line);
|
||||||
|
|
||||||
|
#undef assert
|
||||||
|
#ifdef NDEBUG
|
||||||
|
#define assert(e) ((void)0)
|
||||||
|
#else
|
||||||
|
extern void asserted(int e);
|
||||||
|
#define assert(e) ((void)((e)||(RAISE(assertion_failed),0)))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
extern DWORD except_index;
|
||||||
|
extern void except_init(void);
|
||||||
|
extern void except_push(Except_Frame *fp);
|
||||||
|
extern void except_pop(void);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef WIN32
|
||||||
|
#define RAISE(e) except_raise(&(e), __FILE__, __LINE__)
|
||||||
|
#define RERAISE except_raise(except_frame.exception, \
|
||||||
|
except_frame.file, except_frame.line)
|
||||||
|
#define RETURN switch (except_pop(),0) default: return
|
||||||
|
#define TRY do { \
|
||||||
|
volatile int except_flag; \
|
||||||
|
Except_Frame except_frame; \
|
||||||
|
if (except_index == -1) \
|
||||||
|
except_init(); \
|
||||||
|
except_push(&except_frame); \
|
||||||
|
except_flag = setjmp(except_frame.env); \
|
||||||
|
if (except_flag == EXCEPT_STATE_ENTERED) {
|
||||||
|
#define EXCEPT(e) \
|
||||||
|
if (except_flag == EXCEPT_STATE_ENTERED) except_pop(); \
|
||||||
|
} else if (except_frame.exception == &(e)) { \
|
||||||
|
except_flag = EXCEPT_STATE_HANDLED;
|
||||||
|
#define ELSE \
|
||||||
|
if (except_flag == EXCEPT_STATE_ENTERED) except_pop(); \
|
||||||
|
} else { \
|
||||||
|
except_flag = EXCEPT_STATE_HANDLED;
|
||||||
|
#define FINALLY \
|
||||||
|
if (except_flag == EXCEPT_STATE_ENTERED) except_pop(); \
|
||||||
|
} { \
|
||||||
|
if (except_flag == EXCEPT_STATE_ENTERED) \
|
||||||
|
except_flag = EXCEPT_STATE_FINALIZED;
|
||||||
|
#define END_TRY \
|
||||||
|
if (except_flag == EXCEPT_STATE_ENTERED) except_pop(); \
|
||||||
|
} if (except_flag == EXCEPT_STATE_RAISED) RERAISE; \
|
||||||
|
} while (0)
|
||||||
|
#else
|
||||||
|
#define RAISE(e) except_raise(&(e), __FILE__, __LINE__)
|
||||||
|
#define RERAISE except_raise(except_frame.exception, \
|
||||||
|
except_frame.file, except_frame.line)
|
||||||
|
#define RETURN switch (except_stack = except_stack->prev,0) default: return
|
||||||
|
#define TRY do { \
|
||||||
|
volatile int except_flag; \
|
||||||
|
Except_Frame except_frame; \
|
||||||
|
except_frame.prev = except_stack; \
|
||||||
|
except_stack = &except_frame; \
|
||||||
|
except_flag = setjmp(except_frame.env); \
|
||||||
|
if (except_flag == EXCEPT_STATE_ENTERED) {
|
||||||
|
#define EXCEPT(e) \
|
||||||
|
if (except_flag == EXCEPT_STATE_ENTERED) except_stack = except_stack->prev; \
|
||||||
|
} else if (except_frame.exception == &(e)) { \
|
||||||
|
except_flag = EXCEPT_STATE_HANDLED;
|
||||||
|
#define ELSE \
|
||||||
|
if (except_flag == EXCEPT_STATE_ENTERED) except_stack = except_stack->prev; \
|
||||||
|
} else { \
|
||||||
|
except_flag = EXCEPT_STATE_HANDLED;
|
||||||
|
#define FINALLY \
|
||||||
|
if (except_flag == EXCEPT_STATE_ENTERED) except_stack = except_stack->prev; \
|
||||||
|
} { \
|
||||||
|
if (except_flag == EXCEPT_STATE_ENTERED) \
|
||||||
|
except_flag = EXCEPT_STATE_FINALIZED;
|
||||||
|
#define END_TRY \
|
||||||
|
if (except_flag == EXCEPT_STATE_ENTERED) except_stack = except_stack->prev; \
|
||||||
|
} if (except_flag == EXCEPT_STATE_RAISED) RERAISE; \
|
||||||
|
} while (0)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#endif // except.h
|
||||||
25
include/platform.h
Normal file
25
include/platform.h
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
#ifndef PLATFORM_H
|
||||||
|
#define PLATFORM_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
// Opaque type for a window
|
||||||
|
typedef struct PlatformWindow PlatformWindow;
|
||||||
|
|
||||||
|
// Initializes the platform layer (must be called before creating a window)
|
||||||
|
bool platform_init(void);
|
||||||
|
|
||||||
|
// Creates a window, returns NULL on failure
|
||||||
|
PlatformWindow* platform_create_window(const char* title, int width, int height);
|
||||||
|
|
||||||
|
// Polls platform events. Returns false if the window should close
|
||||||
|
bool platform_poll_events(PlatformWindow* window);
|
||||||
|
|
||||||
|
// Destroys a window
|
||||||
|
void platform_destroy_window(PlatformWindow* window);
|
||||||
|
|
||||||
|
// Shuts down the platform layer
|
||||||
|
void platform_shutdown(void);
|
||||||
|
|
||||||
|
#endif // PLATFORM_H
|
||||||
82
src/except.c
Normal file
82
src/except.c
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include "../include/except.h"
|
||||||
|
|
||||||
|
struct Except_Frame *except_stack = NULL;
|
||||||
|
|
||||||
|
const struct Exception assertion_failed = { "Assertion failed" };
|
||||||
|
void asserted(int e){
|
||||||
|
if(!e){
|
||||||
|
RAISE(assertion_failed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void except_raise(const struct Exception *e, const char *file,int line)
|
||||||
|
{
|
||||||
|
#ifdef WIN32
|
||||||
|
Except_Frame *p;
|
||||||
|
|
||||||
|
if (except_index == TLS_OUT_OF_INDEXES)
|
||||||
|
except_init();
|
||||||
|
p = TlsGetValue(except_index);
|
||||||
|
#else
|
||||||
|
struct Except_Frame *p = except_stack;
|
||||||
|
#endif
|
||||||
|
asserted(e != NULL);
|
||||||
|
if (p == NULL) {
|
||||||
|
fprintf(stderr, "Uncaught exception");
|
||||||
|
if (e->reason)
|
||||||
|
fprintf(stderr, " %s", e->reason);
|
||||||
|
else
|
||||||
|
fprintf(stderr, " at 0x%p", (void*)e);
|
||||||
|
if (file && line > 0)
|
||||||
|
fprintf(stderr, " raised at %s:%d\n", file, line);
|
||||||
|
fprintf(stderr, "aborting...\n");
|
||||||
|
fflush(stderr);
|
||||||
|
abort();
|
||||||
|
}
|
||||||
|
p->exception = e;
|
||||||
|
p->file = file;
|
||||||
|
p->line = line;
|
||||||
|
#ifdef WIN32
|
||||||
|
except_pop();
|
||||||
|
#else
|
||||||
|
except_stack = except_stack->prev;
|
||||||
|
#endif
|
||||||
|
longjmp(p->env, EXCEPT_STATE_RAISED);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef WIN32
|
||||||
|
_CRTIMP void __cdecl _assert(void *, void *, unsigned);
|
||||||
|
#undef assert
|
||||||
|
#define assert(e) ((e) || (_assert(#e, __FILE__, __LINE__), 0))
|
||||||
|
|
||||||
|
DWORD except_index = -1;
|
||||||
|
void except_init(void) {
|
||||||
|
BOOL cond;
|
||||||
|
|
||||||
|
except_index = TlsAlloc();
|
||||||
|
assert(except_index != TLS_OUT_OF_INDEXES);
|
||||||
|
cond = TlsSetValue(except_index, NULL);
|
||||||
|
assert(cond == TRUE);
|
||||||
|
}
|
||||||
|
|
||||||
|
void except_push(Except_Frame *fp) {
|
||||||
|
BOOL cond;
|
||||||
|
|
||||||
|
fp->prev = TlsGetValue(except_index);
|
||||||
|
cond = TlsSetValue(except_index, fp);
|
||||||
|
assert(cond == TRUE);
|
||||||
|
}
|
||||||
|
|
||||||
|
void except_pop(void) {
|
||||||
|
BOOL cond;
|
||||||
|
Except_Frame *tos = TlsGetValue(except_index);
|
||||||
|
|
||||||
|
cond = TlsSetValue(except_index, tos->prev);
|
||||||
|
assert(cond == TRUE);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
78
src/linux_platform.c
Normal file
78
src/linux_platform.c
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
#include "../include/platform.h"
|
||||||
|
#include <X11/Xlib.h>
|
||||||
|
#include <X11/Xutil.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
struct PlatformWindow {
|
||||||
|
Display* display;
|
||||||
|
Window window;
|
||||||
|
Atom wm_delete_window;
|
||||||
|
bool should_close;
|
||||||
|
};
|
||||||
|
|
||||||
|
bool platform_init(void) {
|
||||||
|
return true; // Nothing global needed for X11
|
||||||
|
}
|
||||||
|
|
||||||
|
PlatformWindow* platform_create_window(const char* title, int width, int height) {
|
||||||
|
Display* display = XOpenDisplay(NULL);
|
||||||
|
if (!display) return NULL;
|
||||||
|
|
||||||
|
int screen = DefaultScreen(display);
|
||||||
|
Window win = XCreateSimpleWindow(display, RootWindow(display, screen),
|
||||||
|
0, 0, width, height, 1,
|
||||||
|
BlackPixel(display, screen),
|
||||||
|
WhitePixel(display, screen));
|
||||||
|
|
||||||
|
XStoreName(display, win, title);
|
||||||
|
|
||||||
|
Atom wm_delete_window = XInternAtom(display, "WM_DELETE_WINDOW", False);
|
||||||
|
XSetWMProtocols(display, win, &wm_delete_window, 1);
|
||||||
|
|
||||||
|
XMapWindow(display, win);
|
||||||
|
XFlush(display);
|
||||||
|
|
||||||
|
PlatformWindow* window = malloc(sizeof(PlatformWindow));
|
||||||
|
if (!window) {
|
||||||
|
XDestroyWindow(display, win);
|
||||||
|
XCloseDisplay(display);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
window->display = display;
|
||||||
|
window->window = win;
|
||||||
|
window->wm_delete_window = wm_delete_window;
|
||||||
|
window->should_close = false;
|
||||||
|
|
||||||
|
return window;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool platform_poll_events(PlatformWindow* window) {
|
||||||
|
if (!window) return false;
|
||||||
|
|
||||||
|
while (XPending(window->display)) {
|
||||||
|
XEvent event;
|
||||||
|
XNextEvent(window->display, &event);
|
||||||
|
|
||||||
|
if (event.type == ClientMessage &&
|
||||||
|
(Atom)event.xclient.data.l[0] == window->wm_delete_window) {
|
||||||
|
window->should_close = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return !window->should_close;
|
||||||
|
}
|
||||||
|
|
||||||
|
void platform_destroy_window(PlatformWindow* window) {
|
||||||
|
if (!window) return;
|
||||||
|
|
||||||
|
XDestroyWindow(window->display, window->window);
|
||||||
|
XCloseDisplay(window->display);
|
||||||
|
free(window);
|
||||||
|
}
|
||||||
|
|
||||||
|
void platform_shutdown(void) {
|
||||||
|
// Nothing global needed
|
||||||
|
}
|
||||||
89
src/windows_platform.c
Normal file
89
src/windows_platform.c
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
#include "../include/platform.h"
|
||||||
|
#include <windows.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
struct PlatformWindow {
|
||||||
|
HWND hwnd;
|
||||||
|
bool should_close;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Forward declaration of WndProc
|
||||||
|
static LRESULT CALLBACK window_proc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
|
||||||
|
|
||||||
|
bool platform_init(void) {
|
||||||
|
// Nothing to do for Win32
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
PlatformWindow* platform_create_window(const char* title, int width, int height) {
|
||||||
|
static bool class_registered = false;
|
||||||
|
if (!class_registered) {
|
||||||
|
WNDCLASS wc = {0};
|
||||||
|
wc.lpfnWndProc = window_proc;
|
||||||
|
wc.hInstance = GetModuleHandle(NULL);
|
||||||
|
wc.lpszClassName = "PlatformWindowClass";
|
||||||
|
RegisterClass(&wc);
|
||||||
|
class_registered = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
HWND hwnd = CreateWindowEx(
|
||||||
|
0,
|
||||||
|
"PlatformWindowClass",
|
||||||
|
title,
|
||||||
|
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
|
||||||
|
CW_USEDEFAULT, CW_USEDEFAULT,
|
||||||
|
width, height,
|
||||||
|
NULL, NULL,
|
||||||
|
GetModuleHandle(NULL),
|
||||||
|
NULL
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!hwnd) return NULL;
|
||||||
|
|
||||||
|
PlatformWindow* window = malloc(sizeof(PlatformWindow));
|
||||||
|
if (!window) {
|
||||||
|
DestroyWindow(hwnd);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
window->hwnd = hwnd;
|
||||||
|
window->should_close = false;
|
||||||
|
SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)window);
|
||||||
|
|
||||||
|
return window;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool platform_poll_events(PlatformWindow* window) {
|
||||||
|
MSG msg;
|
||||||
|
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
|
||||||
|
TranslateMessage(&msg);
|
||||||
|
DispatchMessage(&msg);
|
||||||
|
}
|
||||||
|
return !window->should_close;
|
||||||
|
}
|
||||||
|
|
||||||
|
void platform_destroy_window(PlatformWindow* window) {
|
||||||
|
if (!window) return;
|
||||||
|
DestroyWindow(window->hwnd);
|
||||||
|
free(window);
|
||||||
|
}
|
||||||
|
|
||||||
|
void platform_shutdown(void) {
|
||||||
|
// Nothing to do
|
||||||
|
}
|
||||||
|
|
||||||
|
static LRESULT CALLBACK window_proc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
|
||||||
|
PlatformWindow* window = (PlatformWindow*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
|
||||||
|
|
||||||
|
switch (uMsg) {
|
||||||
|
case WM_CLOSE:
|
||||||
|
if (window) window->should_close = true;
|
||||||
|
return 0;
|
||||||
|
case WM_DESTROY:
|
||||||
|
PostQuitMessage(0);
|
||||||
|
return 0;
|
||||||
|
default:
|
||||||
|
return DefWindowProc(hwnd, uMsg, wParam, lParam);
|
||||||
|
}
|
||||||
|
}
|
||||||
17
tests/01_platform.c
Normal file
17
tests/01_platform.c
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
#include "../include/platform.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
if (!platform_init()) return 1;
|
||||||
|
|
||||||
|
PlatformWindow* window = platform_create_window("Hello", 800, 600);
|
||||||
|
if (!window) return 1;
|
||||||
|
|
||||||
|
while (platform_poll_events(window)) {
|
||||||
|
// Your rendering / game loop goes here
|
||||||
|
}
|
||||||
|
|
||||||
|
platform_destroy_window(window);
|
||||||
|
platform_shutdown();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user