Brainstorm is now fully in ruby

This commit is contained in:
John Alanbrook 2022-08-07 06:43:45 +00:00
parent 5b9369a197
commit 406b2b491f
12 changed files with 164 additions and 76 deletions

View file

@ -11,7 +11,6 @@ UNAME_P != uname -m
CCACHE = ccache
#CC specifies which compiler we're using
CC = $(CCACHE) clang -DSDL_DISABLE_IMMINTRIN_H
CLINK = clang

View file

@ -20,6 +20,11 @@ unsigned char temp_bitmap[512 * 512];
struct sFont *font;
static struct mShader *shader;
/*
mfont = MakeFont("notosans.ttf", 300);
text_settype(mfont);
*/
void font_init(struct mShader *textshader) {
shader = textshader;

View file

@ -3,12 +3,14 @@
#include "mruby.h"
#include "mruby/compile.h"
#include "mruby/string.h"
#include "mruby/hash.h"
#include "font.h"
#include "script.h"
#include "string.h"
#include "window.h"
extern mrb_state *mrb;
@ -97,6 +99,40 @@ mrb_value mrb_ui_rendertext(mrb_state *mrb, mrb_value self) {
}
mrb_value mrb_c_reload(mrb_state *mrb, mrb_value self) {
return self;
}
mrb_value mrb_win_make(mrb_state *mrb, mrb_value self) {
char name[50] = "New Window";
struct mSDLWindow *new = MakeSDLWindow(name, 500, 500, 0);
return mrb_float_value(mrb, new->id);
}
mrb_value mrb_nuke_cb(mrb_state *mrb, mrb_value self) {
mrb_float win;
mrb_sym cb;
mrb_get_args(mrb, "fn", &win, &cb);
window_i((int)win)->nuke_cb = cb;
return self;
}
mrb_value mrb_gui_cb(mrb_state *mrb, mrb_value self) {
mrb_float win;
mrb_sym cb;
mrb_get_args(mrb, "fn", &win, &cb);
window_i((int)win)->gui_cb = cb;
return self;
}
mrb_value mrb_sound_make(mrb_state *mrb, mrb_value self) {
mrb_value vals;
mrb_get_args(mrb, "H", &vals);
char *name = mrb_str_to_cstr(mrb, mrb_hash_fetch(mrb, vals, mrb_symbol_value(mrb_intern_cstr(mrb, "name")), mrb_str_new_cstr(mrb, "New Window")));
printf("Window name is %s.\n", name);
return self;
}
mrb_value mrb_sound_cmd(mrb_state *mrb, mrb_value self) {
}
@ -115,5 +151,12 @@ void ffi_load() {
mrb_define_method(mrb, mrb->object_class, "ui_rendertext", mrb_ui_rendertext, MRB_ARGS_REQ(5));
mrb_define_method(mrb, mrb->object_class, "c_reload", mrb_c_reload, MRB_ARGS_REQ(1));
mrb_define_method(mrb, mrb->object_class, "win_make", mrb_win_make, MRB_ARGS_REQ(1));
mrb_define_method(mrb, mrb->object_class, "nuke_cb", mrb_nuke_cb, MRB_ARGS_REQ(2));
mrb_define_method(mrb, mrb->object_class, "gui_cb", mrb_gui_cb, MRB_ARGS_REQ(2));
mrb_define_method(mrb, mrb->object_class, "sound_make", mrb_sound_make, MRB_ARGS_REQ(1));
mrb_define_method(mrb, mrb->object_class, "sound_cmd", mrb_sound_cmd, MRB_ARGS_REQ(2));
}

View file

@ -10,6 +10,7 @@
#define NK_KEYSTATE_BASED_INPUT
#include "nuke.h"
#include "nuklear_glfw_gl3.h"
#include "window.h"

View file

@ -2,7 +2,7 @@
#define NUKE_H
#include "nuklear.h"
#include "nuklear_glfw_gl3.h"
extern struct nk_context *ctx;

View file

@ -172,33 +172,40 @@ void openglInit()
}
void openglRender(struct mSDLWindow *window, struct mCamera *mcamera)
static struct mCamera mcamera = {0};
void openglRender(struct mSDLWindow *window)
{
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.3f, 0.3f, 0.3f, 1.f);
//////////// 2D projection
mfloat_t projection[16] = { 0.f };
mat4_ortho(projection, mcamera->transform.position[0],
window->width + mcamera->transform.position[0],
mcamera->transform.position[1],
window->height + mcamera->transform.position[1], -1.f, 1.f);
mat4_ortho(projection, mcamera.transform.position[0],
window->width + mcamera.transform.position[0],
mcamera.transform.position[1],
window->height + mcamera.transform.position[1], -1.f, 1.f);
glBindBuffer(GL_UNIFORM_BUFFER, projUBO);
glBufferSubData(GL_UNIFORM_BUFFER, 0, 64, projection);
shader_setmat4(vid_shader, "projection", projection);
//shader_setmat4(vid_shader, "projection", projection);
glEnable(GL_DEPTH_TEST);
// Clear color and depth
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
////// TEXT && GUI
script_call_sym(window->gui_cb);
///// Sprites
glDepthFunc(GL_LESS);
shader_use(spriteShader);
sprite_draw_all();
glDepthFunc(GL_ALWAYS);
shader_use(textShader);
shader_setmat4(textShader, "projection", projection);
glDepthFunc(GL_ALWAYS);
shader_use(textShader);
shader_setmat4(textShader, "projection", projection);
}

View file

@ -43,7 +43,7 @@ enum RenderMode {
};
void openglInit();
void openglRender(struct mSDLWindow *window, struct mCamera *camera);
void openglRender(struct mSDLWindow *window);
void openglInit3d(struct mSDLWindow *window);
void openglRender3d(struct mSDLWindow *window, struct mCamera *camera);

View file

@ -3,7 +3,7 @@
#include "stdio.h"
#include "log.h"
#include "mruby.h"
#include "mruby/compile.h"
#include "mrbffi.h"
@ -34,8 +34,8 @@ void script_dofile(const char *file) {
fclose(mrbf);
}
void script_update() {
mrb_funcall(mrb, obj, "update", 0);
void script_update(double dt) {
mrb_funcall(mrb, obj, "update", 1, mrb_float_value(mrb, dt));
mrb_print_error(mrb);
}
@ -50,6 +50,16 @@ void script_editor() {
}
void script_call(const char *f) {
mrb_funcall(mrb, obj, f, 0);
script_call_sym(mrb_intern_cstr(mrb, f));
}
void script_call_sym(mrb_sym sym)
{
if (mrb_respond_to(mrb, obj, sym)) mrb_funcall_argv(mrb, obj, sym, 0, NULL);
mrb_print_error(mrb);
}
int script_has_sym(mrb_sym sym) {
return mrb_respond_to(mrb, obj, sym);
}

View file

@ -1,12 +1,16 @@
#ifndef SCRIPT_H
#define SCRIPT_H
#include "mruby.h"
void script_init();
void script_run(const char *script);
void script_dofile(const char *file);
void script_update();
void script_update(double dt);
void script_draw();
void script_editor();
void script_call(const char *f);
void script_call_sym(mrb_sym sym);
int script_has_sym(mrb_sym sym);
#endif

View file

@ -54,6 +54,34 @@ void window_close_callback(GLFWwindow *w)
quit = 1;
}
void win_key_callback(GLFWwindow *w, int key, int scancode, int action, int mods)
{
char keystr[50] = {'\0'};
strcat(keystr, "input_");
strcat(keystr, glfwGetKeyName(key, 0));
switch (action) {
case GLFW_PRESS:
strcat(keystr, "_down");
break;
case GLFW_RELEASE:
strcat(keystr, "_up");
break;
case GLFW_REPEAT:
strcat(keystr, "_rep");
break;
}
script_call(keystr);
/* Example callback function
if (key == GLFW_KEY_F && action == GLFW_PRESS) {
printf("Pressed F.\n");
if (w == customerWindow->window) window_togglefullscreen(customerWindow);
}
*/
}
struct mSDLWindow *MakeSDLWindow(const char *name, int width, int height, uint32_t flags)
{
struct mSDLWindow *w;
@ -91,6 +119,12 @@ struct mSDLWindow *MakeSDLWindow(const char *name, int width, int height, uint32
glfwSetWindowSizeCallback(w->window, window_size_callback);
glfwSetFramebufferSizeCallback(w->window, window_framebuffer_size_cb);
glfwSetWindowFocusCallback(w->window, window_focus_callback);
glfwSetKeyCallback(w->window, win_key_callback);
nuke_init(w);
w->nuke_cb = 0;
w->gui_cb = 0;
return w;
}
@ -106,7 +140,9 @@ void window_destroy(struct mSDLWindow *w)
vec_delete(&windows, w->id);
}
struct mSDLWindow *window_i(int index) {
return vec_get(&windows, index);
}
void window_handle_event(struct mSDLWindow *w)
{
@ -244,20 +280,18 @@ int window_hasfocus(struct mSDLWindow *w)
return glfwGetWindowAttrib(w->window, GLFW_FOCUSED);
}
double frame_time()
{
return glfwGetTime();
void window_render(struct mSDLWindow *w) {
window_makecurrent(w);
openglRender(w);
if (script_has_sym(w->nuke_cb)) {
nuke_start();
script_call_sym(w->nuke_cb);
nuke_end();
}
window_swap(w);
}
int elapsed_time()
{
static double last_time;
double elapsed;
elapsed = frame_time() - last_time;
last_time = frame_time();
//printf("Elapsed: %d.\n", elapsed);
return elapsed * 1000;
void window_renderall() {
vec_walk(&windows, window_render);
}

View file

@ -5,6 +5,8 @@
#include <stdbool.h>
#include <stdint.h>
#include "mruby.h"
struct mSDLWindow {
GLFWwindow *window;
int id;
@ -19,6 +21,8 @@ struct mSDLWindow {
bool iconified;
bool shown;
float projection[16];
mrb_sym nuke_cb;
mrb_sym gui_cb;
};
struct Texture;
@ -37,8 +41,9 @@ void window_togglefullscreen(struct mSDLWindow *w);
void window_swap(struct mSDLWindow *w);
void window_seticon(struct mSDLWindow *w, struct Texture *icon);
int window_hasfocus(struct mSDLWindow *w);
struct mSDLWindow *window_i(int index);
double frame_time();
int elapsed_time();
void window_render(struct mSDLWindow *w);
void window_renderall();
#endif

View file

@ -3,67 +3,47 @@
#include "window.h"
#include "engine.h"
#include "input.h"
#include "2dphysics.h"
#include "openglrender.h"
#include "gameobject.h"
int physOn = 0;
unsigned int frameCount = 0;
double physMS = sFPS144;
double physlag = 0;
double renderMS = sFPS144;
double renderlag = 0;
struct mCamera camera = {0};
int main(int argc, char **args)
{
camera.speed = 500;
static double renderMS = 0.033;
int main(int argc, char **args) {
engine_init();
struct mSDLWindow *window = MakeSDLWindow("Untitled Game", 1920, 1080, 0);
window_set_icon("icon.png");
script_dofile("game.rb");
openglInit();
editor_init(window);
renderMS = 0.033;
int quit = 0;
double lastTick;
double frameTick;
//While application is running
while (!quit) {
deltaT = elapsed_time();
double elapsed;
double lastTick;
frameTick = glfwGetTime();
elapsed = frameTick - lastTick;
lastTick = frameTick;
physlag += deltaT;
renderlag += deltaT;
renderlag += elapsed;
input_poll();
input_poll();
if (physlag >= physMS) {
phys2d_update(physMS);
window_all_handle_events();
physlag -= physMS;
}
script_update(elapsed);
if (renderlag >= renderMS) {
if (physOn) {
update_gameobjects();
}
if (renderlag >= renderMS) {
renderlag -= renderMS;
window_renderall();
}
camera_2d_update(&camera, renderMS);
openglRender(window, &camera);
editor_render();
window_swap(window);
renderlag -= renderMS;
}
}
engine_stop();
return 0;
}