diff --git a/demo/Demo.cpp b/demo/Demo.cpp index a6894a3..691437d 100644 --- a/demo/Demo.cpp +++ b/demo/Demo.cpp @@ -109,356 +109,10 @@ void set_framerate_indicator(int frame_count, GLuint id) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); } -struct Demo : Game +Mushroom::Mushroom(Node *parent) : Sprite(parent, "resource/shrooms") { - - SDL_Texture *grass_texture; - int frame_count = 0, frame_count_timestamp; - bool right_active = false, down_active = false, left_active = false, - up_active = false; - SDL_Event event; - GLuint vbo, space_texture_id, mvp_id, framerate_texture_id, flat_program, - world_program, fake_texture_id; - glm::mat4 projection, view, model = glm::mat4(1.0f), mvp; - // Mushroom mushroom = Mushroom(this); - // Sprite grass = Sprite(this, "resource/Field.png"); - - Demo() - { - Mix_Music *music = Mix_LoadMUS("resource/Field.mp3"); - Mix_PlayMusic(music, -1); - load_gl_context(); - delegate.subscribe(&Demo::respond, this); - // Input* l = new Input(this); - // delete l; - // input.print_branch(); - // mushroom.print_branch(); - // Input* i = new Input(this); - // get_delegate().unsubscribe(i); - // delete i; - } - - std::string get_class_name() - { - return "Demo"; - } - - void load_sdl_context() - { - Game::load_sdl_context(); - // grass.load(); - // mushroom.load(); - } - - void load_gl_context() - { - Game::load_gl_context(); - // grass.unload(); - // mushroom.unload(); - /* - v0-v1-v2 (front) - v2-v3-v0 - v0-v3-v4 (right) - v4-v5-v0 - v0-v5-v6 (top) - v6-v1-v0 - v1-v6-v7 (left) - v7-v2-v1 - v7-v4-v3 (bottom) - v3-v2-v7 - v4-v7-v6 (back) - v6-v5-v4 - */ - std::array cube = { - { - {1, 1, 1}, {-1, 1, 1}, {-1,-1, 1}, - {-1,-1, 1}, {1,-1, 1}, {1, 1, 1}, - {1, 1, 1}, {1,-1, 1}, {1,-1,-1}, - {1,-1,-1}, {1, 1,-1}, {1, 1, 1}, - {1, 1, 1}, {1, 1,-1}, {-1, 1,-1}, - {-1, 1,-1}, {-1, 1, 1}, {1, 1, 1}, - {-1, 1, 1}, {-1, 1,-1}, {-1,-1,-1}, - {-1,-1,-1}, {-1,-1, 1}, {-1, 1, 1}, - {-1,-1,-1}, {1,-1,-1}, {1,-1, 1}, - {1,-1, 1}, {-1,-1, 1}, {-1,-1,-1}, - {1,-1,-1}, {-1,-1,-1}, {-1, 1,-1}, - {-1, 1,-1}, {1, 1,-1}, {1,-1,-1} - }}; - std::array background_vertices = { - { - {-1, 1, 0}, {1, 1, 0}, {-1, -1, 0}, - {1, 1, 0}, {1, -1, 0}, {-1, -1, 0} - }}; - GLfloat background_colors[40][3] = { - {.2, .6, .8}, {.2, .6, .8}, {1, 1, 0}, - {.2, .6, .8}, {1, 1, 0}, {1, 1, 0} - }; - GLuint background_colors_buffer; - glGenBuffers(1, &background_colors_buffer); - std::array framerate_indicator_vertices = { - { - {.9, 1, 0}, {1, 1, 0}, {.9, .9, 0}, - {1, 1, 0}, {1, .9, 0}, {.9, .9, 0} - }}; - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - std::vector vertices; - vertices.reserve(cube.size() + background_vertices.size() + framerate_indicator_vertices.size()); - vertices.insert(vertices.begin(), cube.begin(), cube.end()); - vertices.insert(vertices.end(), background_vertices.begin(), background_vertices.end()); - vertices.insert(vertices.end(), framerate_indicator_vertices.begin(), - framerate_indicator_vertices.end()); - projection = glm::perspective(glm::radians(45.0f), - (float) sw / (float) sh, 0.1f, 100.0f); - view = glm::lookAt( - glm::vec3(4, 3, 3), glm::vec3(0, 0, 0), glm::vec3(0, 1, 0)); - SDL_Surface *surface = rotateSurface90Degrees(IMG_Load("resource/tile.png"), 2); - printf("tile.png bytes per pixel %i\n", surface->format->BytesPerPixel); - space_texture_id = get_gl_texture_from_surface(surface, GL_LINEAR); - SDL_FreeSurface(surface); - std::array framerate_indicator_uv = { - { - {0, 1}, {1, 1}, {0, 0}, - {1, 1}, {1, 0}, {0, 0} - }}; - std::array cube_uv = { - { - {1, 1}, {0, 1}, {0, 0}, - {0, 0}, {1, 0}, {1, 1}, - {0, 1}, {0, 0}, {1, 0}, - {1, 0}, {1, 1}, {0, 1}, - {0, 1}, {0, 0}, {1, 0}, - {1, 0}, {1, 1}, {0, 1}, - {1, 1}, {0, 1}, {0, 0}, - {0, 0}, {1, 0}, {1, 1}, - {0, 0}, {1, 0}, {1, 1}, - {1, 1}, {0, 1}, {0, 0}, - {0, 0}, {1, 0}, {1, 1}, - {1, 1}, {0, 1}, {0, 0} - }}; - std::vector uv; - uv.reserve(cube_uv.size() + background_vertices.size() + framerate_indicator_uv.size()); - std::copy(cube_uv.begin(), cube_uv.end(), uv.begin()); - std::copy(framerate_indicator_uv.begin(), framerate_indicator_uv.end(), - uv.begin() + cube_uv.size() + background_vertices.size()); - GLuint uvbuffer; - glGenBuffers(1, &uvbuffer); - unsigned char fake_texture_color[4] = {255, 255, 255, 255}; - glCreateTextures(GL_TEXTURE_2D, 1, &fake_texture_id); - glBindTexture(GL_TEXTURE_2D, fake_texture_id); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, - fake_texture_color); - GLuint vao; - glGenVertexArrays(1, &vao); - glBindVertexArray(vao); - glGenBuffers(1, &vbo); - glBindBuffer(GL_ARRAY_BUFFER, vbo); - glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(GLfloat) * 3, &vertices.front(), - GL_STATIC_DRAW); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); - glEnableVertexAttribArray(0); - glBindBuffer(GL_ARRAY_BUFFER, uvbuffer); - glBufferData(GL_ARRAY_BUFFER, uv.capacity() * sizeof(GLfloat) * 2, &uv.front(), GL_STATIC_DRAW); - glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0); - glEnableVertexAttribArray(1); - glBindBuffer(GL_ARRAY_BUFFER, background_colors_buffer); - glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(GLfloat) * 3, 0, GL_STATIC_DRAW); - glBufferSubData(GL_ARRAY_BUFFER, - (cube.size()) * sizeof(GLfloat) * 3, - background_vertices.size() * sizeof(GLfloat) * 3, background_colors); - glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, 0); - glEnableVertexAttribArray(2); - GLuint vertex_shader = load_shader("shaders/triangle.vert", GL_VERTEX_SHADER); - GLuint fragment_shader = load_shader("shaders/triangle.frag", GL_FRAGMENT_SHADER); - GLuint flat_shader = load_shader("shaders/flat.vert", GL_VERTEX_SHADER); - world_program = glCreateProgram(); - glAttachShader(world_program, vertex_shader); - glAttachShader(world_program, fragment_shader); - glBindAttribLocation(world_program, 0, "in_Position"); - glBindAttribLocation(world_program, 1, "vertexUV"); - glBindAttribLocation(world_program, 2, "in_Color"); - glBindAttribLocation(world_program, 3, "pos"); - link_shader(world_program); - flat_program = glCreateProgram(); - glAttachShader(flat_program, flat_shader); - glAttachShader(flat_program, fragment_shader); - glBindAttribLocation(flat_program, 0, "in_Position"); - glBindAttribLocation(flat_program, 1, "vertexUV"); - glBindAttribLocation(flat_program, 2, "in_Color"); - glBindAttribLocation(flat_program, 3, "pos"); - link_shader(flat_program); - mvp_id = glGetUniformLocation(world_program, "MVP"); - GLuint sampler_uniform_id = glGetUniformLocation(world_program, "myTextureSampler"); - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, space_texture_id); - glUniform1i(sampler_uniform_id, 0); - glDepthFunc(GL_LESS); - frame_count_timestamp = SDL_GetTicks(); - framerate_texture_id = get_gl_texture_from_surface( - get_framerate_indicator_surface(frame_count), GL_LINEAR); - } - - void respond(SDL_Event& event) - { - if (delegate.compare(event, "context")) - { - if (is_gl_context) - { - load_sdl_context(); - } - else - { - load_gl_context(); - } - } - } - - void update() - { - // while (SDL_PollEvent(&event)) - // { - // if (event.type == SDL_KEYDOWN) - // { - // if (event.key.keysym.sym == SDLK_F11) - // { - // if (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN) - // { - // SDL_SetWindowFullscreen(window, 0); - // } - // else - // { - // SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN); - // } - // } - // else if (SDL_GetModState() & KMOD_CTRL) - // { - // if (event.key.keysym.sym == SDLK_f) - // { - // show_framerate = not show_framerate; - // } - // else if (event.key.keysym.sym == SDLK_UP) - // { - // set_framerate(framerate + 1); - // } - // else if (event.key.keysym.sym == SDLK_DOWN) - // { - // set_framerate(framerate - 1); - // } - // } - // else if (event.key.keysym.sym == SDLK_UP) - // { - // up_active = true; - // } - // else if (event.key.keysym.sym == SDLK_RIGHT) - // { - // right_active = true; - // } - // else if (event.key.keysym.sym == SDLK_DOWN) - // { - // down_active = true; - // } - // else if (event.key.keysym.sym == SDLK_LEFT) - // { - // left_active = true; - // } - // } - // else if (event.type == SDL_KEYUP) - // { - // if (event.key.keysym.sym == SDLK_UP) - // { - // up_active = false; - // } - // else if (event.key.keysym.sym == SDLK_RIGHT) - // { - // right_active = false; - // } - // else if (event.key.keysym.sym == SDLK_DOWN) - // { - // down_active = false; - // } - // else if (event.key.keysym.sym == SDLK_LEFT) - // { - // left_active = false; - // } - // } - // } - if (is_gl_context) - { - // glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); - // glBufferData(GL_ARRAY_BUFFER, 8 * sizeof(GLfloat), diamond, GL_STATIC_DRAW); - glClearColor(.7, .7, .5, 1); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glDisable(GL_DEPTH_TEST); - glUseProgram(flat_program); - glUniform1f(glGetUniformLocation(flat_program, "r"), mvp[0][0]); - glBindTexture(GL_TEXTURE_2D, fake_texture_id); - glDisableVertexAttribArray(1); - glEnableVertexAttribArray(2); - glDrawArrays(GL_TRIANGLES, 36, 6); - if (show_framerate) - { - glBindTexture(GL_TEXTURE_2D, framerate_texture_id); - glDisableVertexAttribArray(2); - glEnableVertexAttribArray(1); - glVertexAttrib3f(2, 1, 1, 1); - glDrawArrays(GL_TRIANGLES, 42, 6); - } - // printf("%s\n", glm::to_string(model).c_str()); - model = glm::rotate(model, .0005f * frame_length, glm::vec3(0.0f, 1.0f, 0.0f)); - mvp = projection * view * model; - glEnable(GL_DEPTH_TEST); - glUseProgram(world_program); - glUniformMatrix4fv(mvp_id, 1, GL_FALSE, &mvp[0][0]); - glBindTexture(GL_TEXTURE_2D, space_texture_id); - glEnableVertexAttribArray(1); - glDisableVertexAttribArray(2); - glVertexAttrib3f(2, 1, 1, 1); - glDrawArrays(GL_TRIANGLES, 0, 36); - //glFlush(); - SDL_GL_SwapWindow(window); - } - else - { - SDL_SetRenderDrawColor(renderer, 0x0f, 0x4f, 0x8f, 0xff); - SDL_RenderClear(renderer); - roundedBoxColor(renderer, 300, 200, 500, 300, 10, 0x8f8fdfff); - aacircleColor(renderer, 300, 200, 30, 0xffef3fff); - int speed = 2; - // if (up_active) - // { - // grass.move(0, -speed); - // } - // if (right_active) - // { - // grass.move(speed); - // } - // if (down_active) - // { - // grass.move(0, speed); - // } - // if (left_active) - // { - // grass.move(-speed, 0); - // } - // grass.update(); - // mushroom.update(); - SDL_RenderPresent(renderer); - } - frame_count++; - if (ticks - frame_count_timestamp >= 1000) - { - frame_count_timestamp = ticks; - if (is_gl_context and show_framerate) - { - set_framerate_indicator(frame_count, framerate_texture_id); - } - frame_count = 0; - } - } - -}; - -Mushroom::Mushroom(Node *parent) : Sprite(parent, "resource/shrooms") {} + std::cout << "mushroom constructor " << this << std::endl; +} void Mushroom::update() { @@ -480,6 +134,334 @@ void Mushroom::update() Sprite::update(); } +Demo::Demo() +{ + Mix_Music *music = Mix_LoadMUS("resource/Field.mp3"); + Mix_PlayMusic(music, -1); + load_gl_context(); + delegate.subscribe(&Demo::respond, this); + // Input* l = new Input(this); + // delete l; + // input.print_branch(); + // mushroom.print_branch(); + // Input* i = new Input(this); + // get_delegate().unsubscribe(i); + // delete i; +} + +void Demo::load_sdl_context() +{ + Game::load_sdl_context(); + grass.load(); + mushroom.load(); +} + +void Demo::load_gl_context() +{ + Game::load_gl_context(); + grass.unload(); + mushroom.unload(); + /* + v0-v1-v2 (front) + v2-v3-v0 + v0-v3-v4 (right) + v4-v5-v0 + v0-v5-v6 (top) + v6-v1-v0 + v1-v6-v7 (left) + v7-v2-v1 + v7-v4-v3 (bottom) + v3-v2-v7 + v4-v7-v6 (back) + v6-v5-v4 + */ + std::array cube = { + { + {1, 1, 1}, {-1, 1, 1}, {-1,-1, 1}, + {-1,-1, 1}, {1,-1, 1}, {1, 1, 1}, + {1, 1, 1}, {1,-1, 1}, {1,-1,-1}, + {1,-1,-1}, {1, 1,-1}, {1, 1, 1}, + {1, 1, 1}, {1, 1,-1}, {-1, 1,-1}, + {-1, 1,-1}, {-1, 1, 1}, {1, 1, 1}, + {-1, 1, 1}, {-1, 1,-1}, {-1,-1,-1}, + {-1,-1,-1}, {-1,-1, 1}, {-1, 1, 1}, + {-1,-1,-1}, {1,-1,-1}, {1,-1, 1}, + {1,-1, 1}, {-1,-1, 1}, {-1,-1,-1}, + {1,-1,-1}, {-1,-1,-1}, {-1, 1,-1}, + {-1, 1,-1}, {1, 1,-1}, {1,-1,-1} + }}; + std::array background_vertices = { + { + {-1, 1, 0}, {1, 1, 0}, {-1, -1, 0}, + {1, 1, 0}, {1, -1, 0}, {-1, -1, 0} + }}; + GLfloat background_colors[40][3] = { + {.2, .6, .8}, {.2, .6, .8}, {1, 1, 0}, + {.2, .6, .8}, {1, 1, 0}, {1, 1, 0} + }; + GLuint background_colors_buffer; + glGenBuffers(1, &background_colors_buffer); + std::array framerate_indicator_vertices = { + { + {.9, 1, 0}, {1, 1, 0}, {.9, .9, 0}, + {1, 1, 0}, {1, .9, 0}, {.9, .9, 0} + }}; + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + std::vector vertices; + vertices.reserve(cube.size() + background_vertices.size() + framerate_indicator_vertices.size()); + vertices.insert(vertices.begin(), cube.begin(), cube.end()); + vertices.insert(vertices.end(), background_vertices.begin(), background_vertices.end()); + vertices.insert(vertices.end(), framerate_indicator_vertices.begin(), + framerate_indicator_vertices.end()); + projection = glm::perspective(glm::radians(45.0f), + (float) sw / (float) sh, 0.1f, 100.0f); + view = glm::lookAt( + glm::vec3(4, 3, 3), glm::vec3(0, 0, 0), glm::vec3(0, 1, 0)); + SDL_Surface *surface = rotateSurface90Degrees(IMG_Load("resource/tile.png"), 2); + printf("tile.png bytes per pixel %i\n", surface->format->BytesPerPixel); + space_texture_id = get_gl_texture_from_surface(surface, GL_LINEAR); + SDL_FreeSurface(surface); + std::array framerate_indicator_uv = { + { + {0, 1}, {1, 1}, {0, 0}, + {1, 1}, {1, 0}, {0, 0} + }}; + std::array cube_uv = { + { + {1, 1}, {0, 1}, {0, 0}, + {0, 0}, {1, 0}, {1, 1}, + {0, 1}, {0, 0}, {1, 0}, + {1, 0}, {1, 1}, {0, 1}, + {0, 1}, {0, 0}, {1, 0}, + {1, 0}, {1, 1}, {0, 1}, + {1, 1}, {0, 1}, {0, 0}, + {0, 0}, {1, 0}, {1, 1}, + {0, 0}, {1, 0}, {1, 1}, + {1, 1}, {0, 1}, {0, 0}, + {0, 0}, {1, 0}, {1, 1}, + {1, 1}, {0, 1}, {0, 0} + }}; + std::vector uv; + uv.reserve(cube_uv.size() + background_vertices.size() + framerate_indicator_uv.size()); + std::copy(cube_uv.begin(), cube_uv.end(), uv.begin()); + std::copy(framerate_indicator_uv.begin(), framerate_indicator_uv.end(), + uv.begin() + cube_uv.size() + background_vertices.size()); + GLuint uvbuffer; + glGenBuffers(1, &uvbuffer); + unsigned char fake_texture_color[4] = {255, 255, 255, 255}; + glCreateTextures(GL_TEXTURE_2D, 1, &fake_texture_id); + glBindTexture(GL_TEXTURE_2D, fake_texture_id); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, + fake_texture_color); + GLuint vao; + glGenVertexArrays(1, &vao); + glBindVertexArray(vao); + glGenBuffers(1, &vbo); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(GLfloat) * 3, &vertices.front(), + GL_STATIC_DRAW); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); + glEnableVertexAttribArray(0); + glBindBuffer(GL_ARRAY_BUFFER, uvbuffer); + glBufferData(GL_ARRAY_BUFFER, uv.capacity() * sizeof(GLfloat) * 2, &uv.front(), GL_STATIC_DRAW); + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0); + glEnableVertexAttribArray(1); + glBindBuffer(GL_ARRAY_BUFFER, background_colors_buffer); + glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(GLfloat) * 3, 0, GL_STATIC_DRAW); + glBufferSubData(GL_ARRAY_BUFFER, + (cube.size()) * sizeof(GLfloat) * 3, + background_vertices.size() * sizeof(GLfloat) * 3, background_colors); + glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, 0); + glEnableVertexAttribArray(2); + GLuint vertex_shader = load_shader("shaders/triangle.vert", GL_VERTEX_SHADER); + GLuint fragment_shader = load_shader("shaders/triangle.frag", GL_FRAGMENT_SHADER); + GLuint flat_shader = load_shader("shaders/flat.vert", GL_VERTEX_SHADER); + world_program = glCreateProgram(); + glAttachShader(world_program, vertex_shader); + glAttachShader(world_program, fragment_shader); + glBindAttribLocation(world_program, 0, "in_Position"); + glBindAttribLocation(world_program, 1, "vertexUV"); + glBindAttribLocation(world_program, 2, "in_Color"); + glBindAttribLocation(world_program, 3, "pos"); + link_shader(world_program); + flat_program = glCreateProgram(); + glAttachShader(flat_program, flat_shader); + glAttachShader(flat_program, fragment_shader); + glBindAttribLocation(flat_program, 0, "in_Position"); + glBindAttribLocation(flat_program, 1, "vertexUV"); + glBindAttribLocation(flat_program, 2, "in_Color"); + glBindAttribLocation(flat_program, 3, "pos"); + link_shader(flat_program); + mvp_id = glGetUniformLocation(world_program, "MVP"); + GLuint sampler_uniform_id = glGetUniformLocation(world_program, "myTextureSampler"); + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, space_texture_id); + glUniform1i(sampler_uniform_id, 0); + glDepthFunc(GL_LESS); + frame_count_timestamp = SDL_GetTicks(); + framerate_texture_id = get_gl_texture_from_surface( + get_framerate_indicator_surface(frame_count), GL_LINEAR); +} + +void Demo::respond(SDL_Event& event) +{ + if (delegate.compare(event, "context")) + { + if (is_gl_context) + { + load_sdl_context(); + } + else + { + load_gl_context(); + } + } +} + +void Demo::update() +{ + // while (SDL_PollEvent(&event)) + // { + // if (event.type == SDL_KEYDOWN) + // { + // if (event.key.keysym.sym == SDLK_F11) + // { + // if (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN) + // { + // SDL_SetWindowFullscreen(window, 0); + // } + // else + // { + // SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN); + // } + // } + // else if (SDL_GetModState() & KMOD_CTRL) + // { + // if (event.key.keysym.sym == SDLK_f) + // { + // show_framerate = not show_framerate; + // } + // else if (event.key.keysym.sym == SDLK_UP) + // { + // set_framerate(framerate + 1); + // } + // else if (event.key.keysym.sym == SDLK_DOWN) + // { + // set_framerate(framerate - 1); + // } + // } + // else if (event.key.keysym.sym == SDLK_UP) + // { + // up_active = true; + // } + // else if (event.key.keysym.sym == SDLK_RIGHT) + // { + // right_active = true; + // } + // else if (event.key.keysym.sym == SDLK_DOWN) + // { + // down_active = true; + // } + // else if (event.key.keysym.sym == SDLK_LEFT) + // { + // left_active = true; + // } + // } + // else if (event.type == SDL_KEYUP) + // { + // if (event.key.keysym.sym == SDLK_UP) + // { + // up_active = false; + // } + // else if (event.key.keysym.sym == SDLK_RIGHT) + // { + // right_active = false; + // } + // else if (event.key.keysym.sym == SDLK_DOWN) + // { + // down_active = false; + // } + // else if (event.key.keysym.sym == SDLK_LEFT) + // { + // left_active = false; + // } + // } + // } + if (is_gl_context) + { + // glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); + // glBufferData(GL_ARRAY_BUFFER, 8 * sizeof(GLfloat), diamond, GL_STATIC_DRAW); + glClearColor(.7, .7, .5, 1); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glDisable(GL_DEPTH_TEST); + glUseProgram(flat_program); + glUniform1f(glGetUniformLocation(flat_program, "r"), mvp[0][0]); + glBindTexture(GL_TEXTURE_2D, fake_texture_id); + glDisableVertexAttribArray(1); + glEnableVertexAttribArray(2); + glDrawArrays(GL_TRIANGLES, 36, 6); + if (show_framerate) + { + glBindTexture(GL_TEXTURE_2D, framerate_texture_id); + glDisableVertexAttribArray(2); + glEnableVertexAttribArray(1); + glVertexAttrib3f(2, 1, 1, 1); + glDrawArrays(GL_TRIANGLES, 42, 6); + } + // printf("%s\n", glm::to_string(model).c_str()); + model = glm::rotate(model, .0005f * frame_length, glm::vec3(0.0f, 1.0f, 0.0f)); + mvp = projection * view * model; + glEnable(GL_DEPTH_TEST); + glUseProgram(world_program); + glUniformMatrix4fv(mvp_id, 1, GL_FALSE, &mvp[0][0]); + glBindTexture(GL_TEXTURE_2D, space_texture_id); + glEnableVertexAttribArray(1); + glDisableVertexAttribArray(2); + glVertexAttrib3f(2, 1, 1, 1); + glDrawArrays(GL_TRIANGLES, 0, 36); + //glFlush(); + SDL_GL_SwapWindow(window); + } + else + { + SDL_SetRenderDrawColor(renderer, 0x0f, 0x4f, 0x8f, 0xff); + SDL_RenderClear(renderer); + roundedBoxColor(renderer, 300, 200, 500, 300, 10, 0x8f8fdfff); + aacircleColor(renderer, 300, 200, 30, 0xffef3fff); + int speed = 2; + if (up_active) + { + grass.move(0, -speed); + } + if (right_active) + { + grass.move(speed); + } + if (down_active) + { + grass.move(0, speed); + } + if (left_active) + { + grass.move(-speed, 0); + } + grass.update(); + mushroom.update(); + SDL_RenderPresent(renderer); + } + frame_count++; + if (ticks - frame_count_timestamp >= 1000) + { + frame_count_timestamp = ticks; + if (is_gl_context and show_framerate) + { + set_framerate_indicator(frame_count, framerate_texture_id); + } + frame_count = 0; + } +} + int main(int argc, char *argv[]) { Demo demo = Demo(); diff --git a/demo/Demo.hpp b/demo/Demo.hpp index 194c0f2..c8832b7 100644 --- a/demo/Demo.hpp +++ b/demo/Demo.hpp @@ -30,8 +30,8 @@ #include "glm/gtc/matrix_transform.hpp" #include "filesystem.hpp" -#include "Game.hpp" #include "Node.hpp" +#include "Game.hpp" #include "Location.hpp" #include "Sprite.hpp" #include "Input.hpp" @@ -42,8 +42,32 @@ struct Mushroom : Sprite int direction = 1; - Mushroom (Node*); + Mushroom(Node*); void update(); std::string get_class_name() { return "Mushroom"; } }; + +struct Demo : Game +{ + + SDL_Texture *grass_texture; + int frame_count = 0, frame_count_timestamp; + bool right_active = false, down_active = false, left_active = false, + up_active = false; + SDL_Event event; + GLuint vbo, space_texture_id, mvp_id, framerate_texture_id, flat_program, + world_program, fake_texture_id; + glm::mat4 projection, view, model = glm::mat4(1.0f), mvp; + int abc, def, ghi, jkl, mno, pqr, stu, vwx, yz; + Mushroom mushroom = Mushroom(this); + Sprite grass = Sprite(this, "resource/Field.png"); + + Demo(); + void load_sdl_context(); + void load_gl_context(); + void respond(SDL_Event&); + void update(); + std::string get_class_name() { return "Demo"; } + +}; diff --git a/src/Game.cpp b/src/Game.cpp index a0f718e..e49c2d2 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -57,6 +57,7 @@ Game::Game() { print_sdl_error("Could not set up audio"); } + last_frame_timestamp = SDL_GetTicks(); } Game::~Game() diff --git a/src/Node.cpp b/src/Node.cpp index 175a27e..6b3cec9 100644 --- a/src/Node.cpp +++ b/src/Node.cpp @@ -1,6 +1,7 @@ #include "Node.hpp" #include "Game.hpp" #include "Display.hpp" +#include "Delegate.hpp" Node::Node() : Node(NULL) {} diff --git a/src/Recorder.cpp b/src/Recorder.cpp index 7606c71..f35af8a 100644 --- a/src/Recorder.cpp +++ b/src/Recorder.cpp @@ -5,8 +5,6 @@ Recorder::Recorder(Node* parent) : Node(parent) { get_delegate().subscribe(&Recorder::respond, this); - // in_game_stashes.push_back(Stash()); - animation.play(); }