bool EntityType::ImportFromJson( std::istream& FileStream) { // If it isn't a valid file stream, interrupt operation. rapidjson::Document ConfigData; rapidjson::Value WorkVal; std::string StringStream, StringRead; while (getline(FileStream, StringRead)) StringStream += StringRead; ConfigData.Parse(StringStream.c_str()); if (!ConfigData.IsObject()) return false; // Importing general properties ImportJsonData(Properties.Name, ConfigData["Properties"]["Name"]); ImportJsonData(Properties.Type, ConfigData["Properties"]["Type"]); Properties.Description = Properties.Name; ImportJsonData(Properties.Description, ConfigData["Properties"]["Description"]); ImportJsonData(Properties.ShowInCreative, ConfigData["Properties"]["ShowInCreative"]); ImportJsonData(Physics.PhysicsEnabled, ConfigData["Physics"]["PhysicsEnabled"]); ImportJsonData(Physics.CollisionEnabled, ConfigData["Physics"]["CollisionEnabled"]); ImportJsonData(Physics.LengthX, ConfigData["Physics"]["LengthX"]); ImportJsonData(Physics.LengthY, ConfigData["Physics"]["LengthY"]); ImportJsonData(Physics.Mass, ConfigData["Physics"]["Mass"]); // Friction factor, in all 4 faces ImportJsonData(Physics.FrictionFactor[2], ConfigData["Physics"]["FrictionFactorTop"]); Physics.FrictionFactor[3] = Physics.FrictionFactor[1] = Physics.FrictionFactor[0] = Physics.FrictionFactor[2]; ImportJsonData(Physics.FrictionFactor[0], ConfigData["Physics"]["FrictionFactorLeft"]); ImportJsonData(Physics.FrictionFactor[1], ConfigData["Physics"]["FrictionFactorRight"]); ImportJsonData(Physics.FrictionFactor[3], ConfigData["Physics"]["FrictionFactorBottom"]); // Elasticity factor, in all 4 faces. ImportJsonData(Physics.ElasticFactor[2], ConfigData["Physics"]["ElasticFactorTop"]); Physics.ElasticFactor[3] = Physics.ElasticFactor[1] = Physics.ElasticFactor[0] = Physics.ElasticFactor[2]; ImportJsonData(Physics.ElasticFactor[0], ConfigData["Physics"]["ElasticFactorLeft"]); ImportJsonData(Physics.ElasticFactor[1], ConfigData["Physics"]["ElasticFactorRight"]); ImportJsonData(Physics.ElasticFactor[3], ConfigData["Physics"]["ElasticFactorBottom"]); // Miscellaneous properties. ImportJsonData(Physics.BlastResistance, ConfigData["Physics"]["BlastResistance"]); ImportJsonData(Physics.CollisionIgnoreName, ConfigData["Physics"]["CollisionIgnore"]); // Importing general yet self-defined importing procedure properties // Entity class specific properties if (Properties.Type == "Player") { PlayerEntityType* SubData = new PlayerEntityType; ImportJsonData(SubData->MoveSpeed, ConfigData["Properties"]["SpecificProperties"]["MoveSpeed"]); ImportJsonData(SubData->JumpSpeed, ConfigData["Properties"]["SpecificProperties"]["JumpSpeed"]); ImportJsonData(SubData->MaxLife, ConfigData["Properties"]["SpecificProperties"]["MaxLife"]); Properties.SpecificProperties = (void*)SubData; } else if (Properties.Type == "Projectile") { ProjectileEntityType* SubData = new ProjectileEntityType; ImportJsonData(SubData->LaunchSpeed, ConfigData["Properties"]["SpecificProperties"]["LaunchSpeed"]); ImportJsonData(SubData->LaunchInterval, ConfigData["Properties"]["SpecificProperties"]["LaunchInterval"]); ImportJsonData(SubData->DeployDelay, ConfigData["Properties"]["SpecificProperties"]["DeployDelay"]); ImportJsonData(SubData->DeployRadius, ConfigData["Properties"]["SpecificProperties"]["DeployRadius"]); ImportJsonData(SubData->DeployPowerDamage, ConfigData["Properties"]["SpecificProperties"]["DeployPowerDamage"]); ImportJsonData(SubData->DeployPowerMotion, ConfigData["Properties"]["SpecificProperties"]["DeployPowerMotion"]); ImportJsonData(SubData->DeployPowerBlast, ConfigData["Properties"]["SpecificProperties"]["DeployPowerBlast"]); Properties.SpecificProperties = (void*)SubData; } else if (Properties.Type == "Particle") { ParticleEntityType* SubData = new ParticleEntityType; ImportJsonData(SubData->LifeTime, ConfigData["Properties"]["SpecificProperties"]["LifeTime"]); Properties.SpecificProperties = (void*)SubData; } // Import textures list of PNGs and certain graphics properties. if (!ConfigData["Graphics"].IsArray()) return false; for (int Indexer = 0; Indexer < (int)ConfigData["Graphics"].Size(); Indexer++) { typeGraphics nGraphics; ImportJsonData(nGraphics.RenderEnabled, ConfigData["Graphics"][Indexer]["RenderEnabled"]); ImportJsonData(nGraphics.TexRotation, ConfigData["Graphics"][Indexer]["TexRotation"]); ImportJsonData(nGraphics.AnimationInterval, ConfigData["Graphics"][Indexer]["AnimationInterval"]); ImportJsonData(nGraphics.Luminosity, ConfigData["Graphics"][Indexer]["Luminosity"]); ImportJsonData(nGraphics.LengthX, ConfigData["Graphics"][Indexer]["LengthX"]); ImportJsonData(nGraphics.LengthY, ConfigData["Graphics"][Indexer]["LengthY"]); if (ConfigData["Graphics"][Indexer]["TextureList"].IsArray()) { std::vector<std::string> TexturePath; ImportJsonData(TexturePath, ConfigData["Graphics"][Indexer]["TextureList"]); for (auto i = TexturePath.begin(); i != TexturePath.end(); i++) { GLuint Texture; std::string Path = CurrentTextureDirectory + (*i); Texture = LoadPNGTexture(Path.c_str(), NULL, NULL); nGraphics.TextureList.push_back(Texture); } } if (ConfigData["Graphics"][Indexer]["TextureOnHand"].IsString()) { std::string OnHandTex; std::string Path; ImportJsonData(OnHandTex, ConfigData["Graphics"][Indexer]["TextureOnHand"]); Path = CurrentTextureDirectory + OnHandTex; nGraphics.TextureOnHand = LoadPNGTexture(Path.c_str(), NULL, NULL); } else if (nGraphics.TextureList.size() > 0){ nGraphics.TextureOnHand = nGraphics.TextureList[0]; } Graphics.push_back(nGraphics); } // Import trigger operators from JSON if (ConfigData["Physics"]["TriggerList"].IsArray()) { for (unsigned int i = 0; i < ConfigData["Physics"]["TriggerList"].Size(); i++) { Trigger* Worker = new Trigger; if (!ConfigData["Physics"]["TriggerList"][i].IsObject()) continue; if (!Worker->ImportFromJson(ConfigData["Physics"]["TriggerList"][i])) continue; Physics.TriggerList.push_back(Worker); } } return true; }
void InitializeGL() { //Compile the shaders programID = compile_shaders(vshader_square, fshader_square); //Generate Vertex Array and bind the vertex buffer data glGenVertexArrays(1, &VertexArrayID); glBindVertexArray(VertexArrayID); ///--- Generate memory for vertexbuffer GLuint vertexbuffer; glGenBuffers(1, &vertexbuffer); /// The subsequent commands will affect the specified buffer glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); /// Pass the vertex positions to OpenGL glBufferData(GL_ARRAY_BUFFER, sizeof(vpoint), vpoint, GL_STATIC_DRAW); ///--- find the binding point in the shader: /// "vpoint" in the vertex shader glUseProgram(programID); GLuint vpoint_id = glGetAttribLocation(programID, "vpoint"); glEnableVertexAttribArray(vpoint_id); glVertexAttribPointer(vpoint_id, 3, //size per vertex (3 floats for cord) GL_FLOAT, false, //don't normalize 0, //stride = 0 0); //offset = 0 GLuint vtexcoordbuffer; glGenBuffers(1, &vtexcoordbuffer); glBindBuffer(GL_ARRAY_BUFFER, vtexcoordbuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(vtexcoord), vtexcoord, GL_STATIC_DRAW); glUseProgram(programID); //Don't need to do this here again, but it's safe GLuint vtexcoord_binding = glGetAttribLocation(programID, "vtexcoord"); glEnableVertexAttribArray(vtexcoord_binding); glVertexAttribPointer(vtexcoord_binding, 2, GL_FLOAT, false, 0, 0); //Load texture Texture teximage = LoadPNGTexture("texture.png"); //Upload this image onto GPU GLuint texobject; glGenTextures(1, &texobject); glBindTexture(GL_TEXTURE_2D, texobject); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); //the i at the end means to integer glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, teximage.width, teximage.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, teximage.dataptr); // -- Activate the texture image GLuint sampler_ID = glGetUniformLocation(programID, "tex"); glUniform1i(sampler_ID, 0); //Means? If tex is a uniform with type int, then that api call will set this int to 0, but it's a sampler2D, then we tell it to usee the first sampler glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texobject); //Find the binding point for the uniform variable //"rotation" in vertex shader RotBindingID = glGetUniformLocation(programID, "rotation"); }
void Label::Init(int winW, int winH, float posX, float posY, int fontsize, std::string textToDraw) { winWidth = winW; winHeight = winH; ix = posX; iy = posY; iwidth = fontsize; iheight = fontsize; x = posX; y = posY; width = fontsize; height = fontsize; labelText = textToDraw; x /= winWidth; y /= winHeight; width /= winWidth; height /= winHeight; static const GLfloat vertex_positions[] = { -0.5f, 0.5f, 0, -0.5f, -0.5f, 0, 0.5f, -0.5f, 0, 0.5f, -0.5f, 0, 0.5f, 0.5f, 0, -0.5f, 0.5f, 0, }; static const GLushort vertex_indices[] = { 0, 1, 2, 2, 4, 0 }; static const char * vs_source[] = { "#version 410 core \n" " \n" "in vec4 position2; \n" "in vec2 texPos; \n" "out VS_OUT \n" "{ \n" " vec4 color; \n" " vec2 tc; \n" "} vs_out; \n" " \n" "uniform mat4 mv_matrix; \n" "uniform mat4 proj_matrix; \n" "uniform vec4 btnColor; \n" " \n" "const vec2[6] texPos2 = vec2[6](vec2(0.0f, 1.0f), \n" " vec2(0.0f, 0.0f), \n" " vec2(1.0f, 0.0f), \n" " vec2(1.0f, 0.0f), \n" " vec2(1.0f, 1.0f), \n" " vec2(0.0f, 1.0f)); \n" "void main(void) \n" "{ \n" " gl_Position = mv_matrix*position2; \n" " vs_out.tc = texPos.xy; \n" " vs_out.color = btnColor; \n" "} \n" }; static const char * fs_source[] = { "#version 410 core \n" " \n" "out vec4 color; \n" "uniform sampler2D sss; \n" "in VS_OUT \n" "{ \n" " vec4 color; \n" " vec2 tc; \n" "} fs_in; \n" " \n" "void main(void) \n" "{ \n" " color = texture(sss, fs_in.tc )*fs_in.color; \n" "} \n" }; GLuint fs = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fs, 1, fs_source, NULL); glCompileShader(fs); GLuint vs = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vs, 1, vs_source, NULL); glCompileShader(vs); program = glCreateProgram(); glAttachShader(program, vs); glAttachShader(program, fs); glLinkProgram(program); glDeleteShader(vs); glDeleteShader(fs); mv_location = glGetUniformLocation(program, "mv_matrix"); proj_location = glGetUniformLocation(program, "proj_matrix"); btnColor = glGetUniformLocation(program, "btnColor"); glGenVertexArrays(1, &vao); glBindVertexArray(vao); //vertecies glGenBuffers(1, &positionBuffer); glBindBuffer(GL_ARRAY_BUFFER, positionBuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_positions), vertex_positions, GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL); //texture coordinates glGenBuffers(1, &texCoordBuffer); glBindBuffer(GL_ARRAY_BUFFER, texCoordBuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(tex_coords_pos), tex_coords_pos, GL_STATIC_DRAW); glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, NULL); LoadPNGTexture(); glBindVertexArray(0); }