Skip to content

Commit

Permalink
Rename screen window
Browse files Browse the repository at this point in the history
  • Loading branch information
Speak2Erase committed Jul 5, 2024
1 parent f92d28f commit d67b0b0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 37 deletions.
49 changes: 24 additions & 25 deletions binding/osfm-screen-binding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,30 +285,30 @@ class WindowScene : public Scene {
}
};

struct ScreenWindow {
struct MonitorWindow {
SDL_Window* window;
WindowScene scene;

ScreenWindow(int x, int y, int w, int h, unsigned int flags) : scene(w, h) {
MonitorWindow(int x, int y, int w, int h, unsigned int flags) : scene(w, h) {
window = SDL_CreateWindow("Test", x, y, w, h, flags);
}
~ScreenWindow() {
~MonitorWindow() {
if (window)
SDL_DestroyWindow(window);
}

Scene* getScene();
};
// so we can use this from outside this file
Scene* ScreenWindow::getScene() {
Scene* MonitorWindow::getScene() {
return &scene;
}

DEF_TYPE_CUSTOMNAME(ScreenWindow, "Screen::Window");
DEF_TYPE_CUSTOMNAME(MonitorWindow, "MonitorWindow");

#define GUARD_DISPOSED(w) if (!w->window) rb_raise(rb_eRuntimeError, "Window already disposed!");

RB_METHOD(screenWindowInit) {
RB_METHOD(monitorWindowInit) {
VALUE vx, vy, vw, vh;
VALUE kwargs;
rb_scan_args(argc, argv, "4:", &vx, &vy, &vw, &vh, &kwargs);
Expand Down Expand Up @@ -340,24 +340,24 @@ RB_METHOD(screenWindowInit) {
flags |= SDL_WINDOW_FULLSCREEN; // not fullscreen by default
}

ScreenWindow* window = new ScreenWindow(x, y, w, h, flags);
MonitorWindow* window = new MonitorWindow(x, y, w, h, flags);

setPrivateData(self, window);

return self;
}

RB_METHOD(screenWindowDispose) {
ScreenWindow* w = getPrivateData<ScreenWindow>(self);
RB_METHOD(monitorWindowDispose) {
MonitorWindow* w = getPrivateData<MonitorWindow>(self);

delete w;
setPrivateData(self, nullptr);

return Qnil;
}

RB_METHOD(screenWindowDraw) {
ScreenWindow* window = getPrivateData<ScreenWindow>(self);
RB_METHOD(monitorWindowDraw) {
MonitorWindow* window = getPrivateData<MonitorWindow>(self);

GFX_LOCK;

Expand Down Expand Up @@ -394,8 +394,8 @@ RB_METHOD(screenWindowDraw) {
return Qnil;
}

RB_METHOD(screenWindowResize) {
ScreenWindow* window = getPrivateData<ScreenWindow>(self);
RB_METHOD(monitorWindowResize) {
MonitorWindow* window = getPrivateData<MonitorWindow>(self);

GUARD_DISPOSED(window);

Expand All @@ -411,8 +411,8 @@ RB_METHOD(screenWindowResize) {
return Qnil;
}

RB_METHOD(screenWindowMove) {
ScreenWindow* window = getPrivateData<ScreenWindow>(self);
RB_METHOD(monitorWindowMove) {
MonitorWindow* window = getPrivateData<MonitorWindow>(self);

GUARD_DISPOSED(window);

Expand All @@ -425,18 +425,17 @@ RB_METHOD(screenWindowMove) {
}

void osfmBindingInit() {
VALUE module = rb_define_module("Screen");

VALUE klass = rb_define_class_under(module, "Window", rb_cObject);
rb_define_alloc_func(klass, classAllocate<&ScreenWindowType>);
VALUE klass = rb_define_class("MonitorWindow", rb_cObject);
rb_define_alloc_func(klass, classAllocate<&MonitorWindowType>);

_rb_define_method(klass, "initialize", screenWindowInit);
_rb_define_method(klass, "dispose", screenWindowDispose);
_rb_define_method(klass, "draw", screenWindowDraw);
_rb_define_method(klass, "resize", screenWindowResize);
_rb_define_method(klass, "move", screenWindowMove);
_rb_define_method(klass, "initialize", monitorWindowInit);
_rb_define_method(klass, "dispose", monitorWindowDispose);
_rb_define_method(klass, "draw", monitorWindowDraw);
_rb_define_method(klass, "resize", monitorWindowResize);
_rb_define_method(klass, "move", monitorWindowMove);

rb_define_const(module, "UNDEFINED_POS", INT2NUM(SDL_WINDOWPOS_UNDEFINED));
rb_define_const(module, "CENTERED_POS", INT2NUM(SDL_WINDOWPOS_CENTERED));
rb_define_const(klass, "UNDEFINED_POS", INT2NUM(SDL_WINDOWPOS_UNDEFINED));
rb_define_const(klass, "CENTERED_POS", INT2NUM(SDL_WINDOWPOS_CENTERED));

}
24 changes: 12 additions & 12 deletions binding/viewport-binding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ DEF_ALLOCFUNC(Viewport);

// oh yes. this is awful. there should be a header for this
// more fun this way :3
extern rb_data_type_t ScreenWindowType;
struct ScreenWindow {
extern rb_data_type_t MonitorWindowType;
struct MonitorWindow {
Scene* getScene();
};

Expand All @@ -53,33 +53,33 @@ RB_METHOD(viewportInitialize) {
// could either be a Rect or a Window
} else if (argc == 1 || argc == 2) {
VALUE rectObj;
VALUE screenWindowObj = Qnil;
VALUE monitorWindowObj = Qnil;
Rect *rect;

rb_get_args(argc, argv, "o|o", &rectObj, &screenWindowObj RB_ARG_END);
rb_get_args(argc, argv, "o|o", &rectObj, &monitorWindowObj RB_ARG_END);

rect = getPrivateDataCheck<Rect>(rectObj, RectType);

Scene *scene = nullptr;
if (!NIL_P(screenWindowObj)) {
ScreenWindow* window = getPrivateDataCheck<ScreenWindow>(screenWindowObj, ScreenWindowType);
if (!NIL_P(monitorWindowObj)) {
MonitorWindow* window = getPrivateDataCheck<MonitorWindow>(monitorWindowObj, MonitorWindowType);
scene = window->getScene();
rb_iv_set(self, "screen_window", screenWindowObj); // so it doesn't get GC'd
rb_iv_set(self, "screen_window", monitorWindowObj); // so it doesn't get GC'd
}

GFX_LOCK;
v = new Viewport(rect, scene);
} else {
int x, y, width, height;
VALUE screenWindowObj = Qnil;
VALUE monitorWindowObj = Qnil;

rb_get_args(argc, argv, "iiii|o", &x, &y, &width, &height, &screenWindowObj RB_ARG_END);
rb_get_args(argc, argv, "iiii|o", &x, &y, &width, &height, &monitorWindowObj RB_ARG_END);

Scene *scene = nullptr;
if (!NIL_P(screenWindowObj)) {
ScreenWindow* window = getPrivateDataCheck<ScreenWindow>(screenWindowObj, ScreenWindowType);
if (!NIL_P(monitorWindowObj)) {
MonitorWindow* window = getPrivateDataCheck<MonitorWindow>(monitorWindowObj, MonitorWindowType);
scene = window->getScene();
rb_iv_set(self, "screen_window", screenWindowObj); // so it doesn't get GC'd
rb_iv_set(self, "screen_window", monitorWindowObj); // so it doesn't get GC'd
}

GFX_LOCK;
Expand Down

0 comments on commit d67b0b0

Please sign in to comment.