Minor performance improvements.
This commit is contained in:
parent
647ca6084c
commit
189e840f25
5 changed files with 388 additions and 50 deletions
114
src/Main.cpp
114
src/Main.cpp
|
@ -1,4 +1,5 @@
|
|||
#include <string>
|
||||
#include <SDL_video.h>
|
||||
#include "Main.hpp"
|
||||
|
||||
|
||||
|
@ -9,9 +10,9 @@ void logSDLError(std::ostream &os, const std::string &msg)
|
|||
|
||||
SDL_Window *init_window()
|
||||
{
|
||||
|
||||
SDL_Window *window = SDL_CreateWindow("Hello World!", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
|
||||
SCREEN_WIDTH,
|
||||
SCREEN_HEIGHT, SDL_WINDOW_OPENGL);
|
||||
SCREEN_WIDTH, SCREEN_HEIGTH, SDL_WINDOW_OPENGL);
|
||||
if (window == NULL)
|
||||
{
|
||||
logSDLError(std::cout, "SDL_CreateWindow");
|
||||
|
@ -32,50 +33,120 @@ SDL_Renderer *init_renderer(SDL_Window *window)
|
|||
return renderer;
|
||||
}
|
||||
|
||||
SDL_Surface *init_surface(int width, int height)
|
||||
void Timer::start_timer()
|
||||
{
|
||||
|
||||
//SDL_Surface *surface = SDL_CreateRGBSurface(0, width, height, 32, rmask, gmask, bmask, amask);
|
||||
SDL_Surface *surface = SDL_CreateRGBSurface(0, width, height, 32, 0, 0, 0, 0);
|
||||
if (surface == NULL)
|
||||
{
|
||||
logSDLError(std::cout, "SDL_CreateSurface");
|
||||
}
|
||||
return surface;
|
||||
this->timer_started = SDL_GetTicks();
|
||||
}
|
||||
|
||||
SDL_Texture *init_texture(SDL_Renderer *renderer, SDL_Surface *surface)
|
||||
Uint32 Timer::get_timer()
|
||||
{
|
||||
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
|
||||
if (texture == NULL)
|
||||
Uint32 ticks_passed = SDL_GetTicks() - this->timer_started;
|
||||
return ticks_passed;
|
||||
}
|
||||
|
||||
Uint32 Timer::reset_timer()
|
||||
{
|
||||
Uint32 ticks_passed = this->get_timer();
|
||||
this->timer_started = SDL_GetTicks();
|
||||
return ticks_passed;
|
||||
}
|
||||
|
||||
void Game::toggle_fullscreen()
|
||||
{
|
||||
SDL_DisplayMode dm;
|
||||
SDL_GetCurrentDisplayMode(SDL_GetWindowDisplayIndex(this->window), &dm);
|
||||
if (!this->full_screen)
|
||||
{
|
||||
logSDLError(std::cout, "SDL_CreateTexture");
|
||||
this->full_screen = true;
|
||||
SDL_SetWindowSize(this->window, dm.w, dm.h);
|
||||
SDL_SetWindowFullscreen(this->window, SDL_WINDOW_FULLSCREEN);
|
||||
}
|
||||
else
|
||||
{
|
||||
this->full_screen = false;
|
||||
SDL_SetWindowFullscreen(this->window, 0);
|
||||
SDL_SetWindowSize(this->window, SCREEN_WIDTH, SCREEN_HEIGTH);
|
||||
SDL_SetWindowPosition(this->window, dm.w / 4, dm.h / 4);
|
||||
}
|
||||
}
|
||||
|
||||
void Game::handle_event(SDL_Event *event)
|
||||
{
|
||||
if (event->type == SDL_KEYDOWN)
|
||||
{
|
||||
SDL_Keycode key = event->key.keysym.sym;
|
||||
if (key == SDLK_w)
|
||||
this->move[0] = true;
|
||||
if (key == SDLK_a)
|
||||
this->move[1] = true;
|
||||
if (key == SDLK_s)
|
||||
this->move[2] = true;
|
||||
if (key == SDLK_d)
|
||||
this->move[3] = true;
|
||||
if (key == SDLK_f)
|
||||
{
|
||||
toggle_fullscreen();
|
||||
}
|
||||
if (key == SDLK_ESCAPE)
|
||||
{
|
||||
this->quit = true;
|
||||
}
|
||||
}
|
||||
if (event->type == SDL_KEYUP)
|
||||
{
|
||||
SDL_Keycode key = event->key.keysym.sym;
|
||||
if (key == SDLK_w)
|
||||
this->move[0] = false;
|
||||
if (key == SDLK_a)
|
||||
this->move[1] = false;
|
||||
if (key == SDLK_s)
|
||||
this->move[2] = false;
|
||||
if (key == SDLK_d)
|
||||
this->move[3] = false;
|
||||
}
|
||||
return texture;
|
||||
}
|
||||
|
||||
int Game::game_loop()
|
||||
{
|
||||
bool quit = false;
|
||||
while (!quit)
|
||||
Timer *frame_timer = new Timer();
|
||||
frame_timer->start_timer();
|
||||
double fps;
|
||||
Uint32 frame_counter = 0;
|
||||
while (!this->quit)
|
||||
{
|
||||
if (frame_timer->get_timer() > 1000.0)
|
||||
{
|
||||
fps = frame_counter / (frame_timer->reset_timer() / 1000.0);
|
||||
frame_counter = 0;
|
||||
std::cout << fps << std::endl;
|
||||
}
|
||||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event))
|
||||
{
|
||||
if (event.type == SDL_KEYDOWN || event.type == SDL_KEYUP)
|
||||
{
|
||||
this->handle_event(&event);
|
||||
}
|
||||
if (event.type == SDL_QUIT)
|
||||
{
|
||||
quit = true;
|
||||
} else if (event.type == SDL_MOUSEBUTTONDOWN)
|
||||
break;
|
||||
}
|
||||
if (event.type == SDL_MOUSEMOTION || event.type == SDL_MOUSEWHEEL)
|
||||
{
|
||||
quit = true;
|
||||
grid->handle_event(&event);
|
||||
}
|
||||
}
|
||||
SDL_Point move_by = {(this->move[1] - this->move[3]) * 10, (this->move[0] - this->move[2]) * 10};
|
||||
this->grid->move(move_by);
|
||||
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00);
|
||||
SDL_RenderClear(renderer);
|
||||
SDL_SetRenderDrawColor(renderer, 0xff, 0xff, 0xff, 0xff);
|
||||
this->grid->render(renderer);
|
||||
SDL_RenderPresent(renderer);
|
||||
frame_counter++;
|
||||
}
|
||||
delete frame_timer;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -104,6 +175,9 @@ int main(int, char **)
|
|||
Game *game = new Game(window, renderer);
|
||||
int exit_status = game->game_loop();
|
||||
delete game;
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
||||
return exit_status;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue