spacebox/src/Game.hpp

109 lines
2.4 KiB
C++
Raw Normal View History

2019-04-23 01:42:19 -04:00
#ifndef Game_h_
#define Game_h_
#include <string>
#include <iostream>
#include <list>
#include <sstream>
#define SDL_MAIN_HANDLED
#include <SDL.h>
#include <SDL_mixer.h>
#include <SDL_ttf.h>
#define GL_GLEXT_PROTOTYPES
#define GLEW_STATIC
#if defined(__EMSCRIPTEN__)
#include <emscripten.h>
#include <emscripten/html5.h>
#include <GL/glew.h>
#else
2019-04-29 19:27:13 -04:00
#include "glew/glew.h"
2019-04-23 01:42:19 -04:00
#endif
2019-04-23 01:42:19 -04:00
#include "Node.hpp"
#include "Input.hpp"
#include "Recorder.hpp"
#include "Sprite.hpp"
2019-06-16 03:05:02 -04:00
struct FramerateIndicator : Sprite
{
2019-06-16 03:05:02 -04:00
FramerateIndicator(Node*);
void respond(SDL_Event&);
SDL_Surface* get_surface();
void refresh();
2019-06-16 03:05:02 -04:00
};
2019-04-23 01:42:19 -04:00
struct Game : Node
{
Game(const Game&) = delete;
Game& operator=(const Game&) = delete;
Game(Game&&) = delete;
Game& operator=(Game&&) = delete;
2019-05-07 03:33:54 -04:00
SDL_Window* window;
SDL_Renderer* renderer = NULL;
2019-04-23 01:42:19 -04:00
SDL_GLContext glcontext = NULL;
2019-06-16 03:05:02 -04:00
// 768, 432
// 864, 486
int frame_count_this_second = 0, framerate, ticks, last_frame_length;
float frame_length = 1000.0 / 60.0, frame_time_overflow = 0, last_frame_timestamp,
last_frame_count_timestamp, emscripten_previous_time;
bool done = false, show_framerate = true, is_gl_context = true;
Configuration configuration = Configuration(this);
Delegate delegate = Delegate(this);
Display display = Display(this);
Recorder recorder = Recorder(this);
Input input = Input(this);
std::vector<float> frame_length_history;
TTF_Font* bp_mono_font = NULL;
FramerateIndicator framerate_indicator = FramerateIndicator(this);
2019-04-23 01:42:19 -04:00
Game();
~Game();
2019-04-23 01:42:19 -04:00
void print_error(std::string);
void print_sdl_error(std::string);
void print_gl_attributes();
void print_frame_length_history();
2019-04-23 01:42:19 -04:00
void load_sdl_context();
void load_gl_context();
bool log_gl_errors(std::string);
void log_display_mode();
2019-06-16 03:05:02 -04:00
void log_surface_format(SDL_Surface*, std::string = "surface");
std::string get_pixel_format_string(Uint32);
SDL_Window* get_window();
SDL_Renderer* get_renderer();
2019-04-23 01:42:19 -04:00
void run();
void frame(float);
2019-04-23 01:42:19 -04:00
void flag_to_end();
2019-05-03 02:09:48 -04:00
virtual void update() {};
glm::vec2 weight(glm::vec2);
2019-04-23 01:42:19 -04:00
void set_framerate(int);
2019-05-04 03:25:35 -04:00
void handle_quit_event(SDL_Event&);
2019-04-23 01:42:19 -04:00
void quit();
2019-05-02 20:11:45 -04:00
std::string get_class_name() { return "Game"; }
2019-04-23 01:42:19 -04:00
template<typename T>
float weight(T amount)
2019-04-23 01:42:19 -04:00
{
return (last_frame_length / (1000.0 / 60)) * amount;
}
};
#if defined(__EMSCRIPTEN__)
void loop(void*);
#endif
2019-04-23 01:42:19 -04:00
#endif