Skip to content

Commit

Permalink
actually, make cxx bindings part of the main header
Browse files Browse the repository at this point in the history
  • Loading branch information
zserge committed Feb 10, 2023
1 parent 959d3ce commit 63e84e0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 36 deletions.
39 changes: 3 additions & 36 deletions examples/minimal-cxx/main.cc
Original file line number Diff line number Diff line change
@@ -1,38 +1,5 @@
#include "fenster.h"

class Fenster {
struct fenster f;
int64_t now;

public:
Fenster(const int w, const int h, const char *title)
: f{.title = title, .width = w, .height = h} {
this->f.buf = new uint32_t[w * h];
this->now = fenster_time();
fenster_open(&this->f);
}
~Fenster() {
fenster_close(&this->f);
delete[] this->f.buf;
}
bool loop(const int fps) {
int64_t t = fenster_time();
if (t - this->now < 1000 / fps) {
fenster_sleep(t - now);
}
this->now = t;
return fenster_loop(&this->f) == 0;
}
inline uint32_t &px(const int x, const int y) {
return fenster_pixel(&this->f, x, y);
}
bool key(int c) { return c >= 0 && c < 128 ? this->f.keys[c] : false; }
int x() { return this->f.x; }
int y() { return this->f.y; }
int mouse() { return this->f.mouse; }
int mod() { return this->f.mod; }
};

#define W 320
#define H 240

Expand All @@ -53,9 +20,9 @@ static int run() {
f.px(i, j) = i ^ j ^ t;
}
}
if (f.key(0x1b)) {
break;
}
if (f.key(0x1b)) {
break;
}
t++;
}
return 0;
Expand Down
35 changes: 35 additions & 0 deletions fenster.h
Original file line number Diff line number Diff line change
Expand Up @@ -328,5 +328,40 @@ FENSTER_API int64_t fenster_time() {
}
#endif

#ifdef __cplusplus
class Fenster {
struct fenster f;
int64_t now;

public:
Fenster(const int w, const int h, const char *title)
: f{.title = title, .width = w, .height = h} {
this->f.buf = new uint32_t[w * h];
this->now = fenster_time();
fenster_open(&this->f);
}
~Fenster() {
fenster_close(&this->f);
delete[] this->f.buf;
}
bool loop(const int fps) {
int64_t t = fenster_time();
if (t - this->now < 1000 / fps) {
fenster_sleep(t - now);
}
this->now = t;
return fenster_loop(&this->f) == 0;
}
inline uint32_t &px(const int x, const int y) {
return fenster_pixel(&this->f, x, y);
}
bool key(int c) { return c >= 0 && c < 128 ? this->f.keys[c] : false; }
int x() { return this->f.x; }
int y() { return this->f.y; }
int mouse() { return this->f.mouse; }
int mod() { return this->f.mod; }
};
#endif /* __cplusplus */

#endif /* !FENSTER_HEADER */
#endif /* FENSTER_H */

0 comments on commit 63e84e0

Please sign in to comment.