What's new

Closed [lua c++] explicit vs implicit context object

Status
Not open for further replies.

nnivx

Honorary Poster
Joined
Mar 16, 2017
Posts
164
Reaction
88
Points
128
I am creating an API in C++ for our game which will be called from lua. The scripts will be dynamically loaded to speed up testing and prototyping.

We're creating the game from scratch and I am using OpenGL and shaders to render graphics. In particular we want to export functions like these:

testscript.lua
Code:
params = {}

-- name of the shader object
params.name = "simple"

-- load vertex shader
params.vert = GetFileContents("simple.vert")

-- conveniently code glsl directly
params.frag = [[
#version 130

in vec3 color;

void main() {
    gl_FragColor = vec4(color, 1.0);
}]]

local shader = CreateShader(params)
Pero gusto ko i-introduce yung concept ng contexts - not the "state" or "context" object you usually pass around as first parameter in c - but rather the scope in which functions can be called.

I want CreateShader() to work only during initialization phase, but after the OpenGL context had been created calling it everywhere else would be îllégâl. I have this struct in C++:
lua_AIPstate.h
Code:
struct AIPstate {
   enum {
      PreInit,    // pre-initialization no opengl context is active yet
      Init,       // this is where CreateShader() is legal
      Update, 
      Render
   } context;
   GLFWwindow* window;
   // ...
};
This should be enough to validate function calls, however the problem is *how* to pass this struct around.
These were the options I gathered:

a) put this struct in lua registry and,
b) pass a context object around
Yung unang option parang maganda, pero may consistent overhead sa mga context-sensitive functions. Yung second option naman eh boilerplate sa lua script:
Code:
function Initialize(context)
    -- passing 'context' around reduces productivity and more opportunity for bugs
    shader = CreateShader(context, params)
    invProj = GetShaderUniform(context, shader, "invProj")
    pos = GetShaderAttribute(context, shader, "pos")
end
So alin yung gamit niyo sa game engine? Do you have better ideas to implement such feature? Kaya ko rin i-modify yung lua para ma-embed yung contexts natively pero ma dedelay kami... illustrator din kasi ako :D

references:
You do not have permission to view the full content of this post. Log in or register now.



 
Share po the beta program not the script hehe ng maitry
see attachment for screenshot, wala pang working beta na "game" :D core (math library, graphics, system) pa lang po natatapos ko, wala pa yung game mismo haha.

di ko kasi ma-upload yung repo sa github.. ayaw mag-connect pag shadowsocks gamit
 

Attachments

Status
Not open for further replies.
Back
Top