Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 07 Oct 2011
Offline Last Active Oct 15 2016 09:12 PM

Posts I've Made

In Topic: OpenGL, oh my god

11 September 2016 - 09:09 PM

You can get a simple base going on with some basic setup code. Download GLEW and get that working if you haven't already.

I will post some code without error checking, and hopefully this step by step will help you understand better.


This is not a full example, and there might even be a typo in here, but if you start from the main function and read what's happening line by line, you will come to an understanding of how it all comes together.


Keep in mind the shader must have input attributes 0: vec4 position, 1: vec4 color and 2: vec2 uv.


This is intended to be a reference if you're stuck, not a complete example.


SDL_GLContext gl_context = nullptr;


std::string read_file(std::string path) {
    std::ifstream file(path, std::ios::binary);
    if (!file.is_open()) { return ""; }
    std::stringstream result;
    result << file.rdbuf();
    return result.str();

void init_gl(SDL_Window* window) {

    gl_context = SDL_GL_CreateContext(window);







int load_shader(int type, std::string path) {

    int id = glCreateShader(type);

    const char* source = read_file(path).c_str();

    glShaderSource(id, 1, &source, 0);


    return id; // look at glGetShaderInfoLog later on



int load_shader_program(std::string path_vert, std::string path_frag) {

    int shader_program_id = glCreateProgram();

    int vert_id = load_shader(GL_VERTEX_SHADER, path_vert);

    int frag_id = load_shader(GL_FRAGMENT_SHADER, path_frag);

    glAttachShader(shader_program_id, vert_id);

    glAttachShader(shader_program_id, frag_id);


    // look at glGetProgramInfoLog, glValidateProgram and glGetProgramiv later on

    return shader_program_id;



struct vector4f { float x = 0.0f, y = 0.0f, z = 0.0f, w = 0.0f; };

struct vector3f { float x = 0.0f, y = 0.0f, z = 0.0f; };

struct vector2f { float x = 0.0f, y = 0.0f; };


struct gl_vertex {

    vector4f position;

    float r = 1.0f, g = 1.0f, b = 1.0f, a = 1.0f;

    vector2f uv;



struct gl_shape {

    int vao = 0;

    int index_buffer = 0, vertex_buffer = 0;

    std::vector<unsigned int> indices;

    std::vector<gl_vertex> vertices;



void use_gl_shape(gl_shape* shape) {


    glBindBuffer(GL_ARRAY_BUFFER, shape->index_buffer);

    glBindBuffer(GL_ARRAY_BUFFER, shape->vertex_buffer);



gl_shape create_quad() {

    gl_shape quad;

    quad.indices = { 0, 1, 2, 3, 2, 0 };

    quad.vertices.insert(quad.vertices.begin(), 4, gl_vertex());

    quad.vertices[0].position = { 0.0f, 0.0f, 0.0f, 1.0f };

    quad.vertices[1].position = { 1.0f, 0.0f, 0.0f, 1.0f };

    quad.vertices[2].position = { 1.0f, 1.0f, 0.0f, 1.0f };

    quad.vertices[3].position = { 0.0f, 1.0f, 0.0f, 1.0f };

    quad.vertices[0].uv = { 0.0f, 0.0f };

    quad.vertices[1].uv = { 1.0f, 0.0f };

    quad.vertices[2].uv = { 1.0f, 1.0f };

    quad.vertices[3].uv = { 0.0f, 1.0f };

    glGenVertexArrays(1, &quad.vao);

    glGenBuffers(1, &quad.index_buffer);

    glGenBuffers(1, &quad.vertex_buffer);


    glBufferData(GL_ARRAY_BUFFER, sizeof(unsigned int) * quad.indices.size(), &quad.indices[0], GL_STATIC_DRAW);
    glBufferData(GL_ARRAY_BUFFER, sizeof(gl_vertex) * quad.vertices.size(), &quad.vertices[0], GL_STATIC_DRAW);
    glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(gl_vertex), nullptr);
    glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(gl_vertex), (void*) (4 * sizeof(float)));
    glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(gl_vertex), (void*) (8 * sizeof(float)));

    return quad;



int main() {

    //   ... setup ...


    int shader_program = load_shader_program("shader.vert", "shader.frag");

    gl_shape quad = create_quad();

    glClearColor(0.2f, 0.2f, 0.2f, 1.0f);

    while (true) {



        // <- This is where you set your projection and view matrix uniforms ->

        // ...

        // <- This is where you set your model matrix uniform ->


        glDrawElements(GL_TRIANGLES, shape.indices.size(), GL_UNSIGNED_INT, &shape.indices[0]);



    // ... cleanup ...

    return 0;




 If you need help figuring out projection and view matrices, feel free to ask.

In Topic: Multiple bullets?

17 April 2016 - 11:09 AM


Anyway, look into "std::vector".

Yes, you would be correct if the OP was using C++. But the tags on his topic states “Java,” so he should use java.util.ArrayList instead.


Oh my, I didn't catch that.

In Topic: Multiple bullets?

16 April 2016 - 10:09 AM

then it becomes null




Anyway, look into "std::vector".



You could just make a variable like this:

std::vector<Bullet> myBullets;

then use it like this:

myBullets.push_back(Bullet(x, y)); // or however your constructor goes, if any. You can probably do myBullets.push_back({ x, y }); as well

If you have update and draw functions in your bullet class, then you can do this to call those:

for (auto& i : myBullets) { // Loop through all the elements in "myBullet" and let "i" be a reference to the current element. 
    i.update();  // or i.draw();

Hope that helps in any way.

In Topic: C++ Passing an unknown class as an argument to a function

26 October 2015 - 05:45 AM

Lets say i want a function to take a class as a parameter but i do not know the class in advance or i want it to be able to take different classes, how can i do it?

Class object;
AnotherClass antherObject;

function(Class &object)

How can i make "function" take "AnotherClass" as a parameter as well? Overloading is a solution but i would still have to copy the code again. Is there another way?


The question is kind of vague to get a good answer.

Sure, you can use templates or polymorphism - but is that really what you need?


If you have a specific scenario in mind, I'd advise you to post that and see what response you get then.

Sometimes you might only need to pass a few common variables instead of the class itself.



Oops, I realised now that Lactose wrote the same thing :)

In Topic: Off shore Development- What to get once the job is done

25 October 2015 - 10:54 AM

Hi Lactose,

Please clarify the following


(1) Will he be able to make any changes to the app which is UP and running on the market place without logging into my developer account? I don't think so, but want to reconfirm.


(2) It looks like it is better to have the source code in case if i want to make any changes in the future. In this case, what kind of format i can ask him to send me the source code?


(3) I am not a programmer and don't know ABCD of game programming. So even if sends some thing, i cant really make sure that this the source code for this program. What are my options in this case except trusting him thinking that he is sending the right code?




1. No


2. There is not really a format. As long as you get the code, you should be fine. If he has some fancy project setup, then it would be great if he sent you those or told you how it was set up.


3. I doubt someone would do that. Especially if you have made a game with him. Anyway, I would open the source files and see if you can find things you recognise.

For example, see if you find the code for the player or the code for the main menu or enemies. Just read the code a bit like English, and you should figure out what it does.

If you have no idea after that, you should perhaps either trust someone else to look over it, or learn to build the project yourself given you have the project files.



Got ninja'd. Lactose probably has a better answer than mine though.