node and game structs

This commit is contained in:
Frank DeMarco 2019-04-23 01:42:19 -04:00
parent f5ace066ba
commit c8b29cfdbe
9 changed files with 403 additions and 216 deletions

178
Game.cpp Normal file
View File

@ -0,0 +1,178 @@
#include "Game.hpp"
Game::Game()
{
std::cout << "GLEW " << glewGetString(GLEW_VERSION) << std::endl;
putenv("SDL_VIDEO_X11_LEGACY_FULLSCREEN=0");
putenv("SDL_VIDEO_CENTERED=1");
SDL_version version;
SDL_GetVersion(&version);
printf("SDL %d.%d.%d\n", version.major, version.minor, version.patch);
fprintf(stderr, "stderr test message\n");
SDL_SetMainReady();
// SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
print_gl_attributes();
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
{
print_sdl_error("SDL could not initialize");
flag_to_end();
}
// SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
print_gl_attributes();
window = SDL_CreateWindow("TARE control", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
sw, sh, SDL_WINDOW_OPENGL);
if (window == NULL)
{
print_sdl_error("Could not create window");
flag_to_end();
}
SDL_ShowCursor(0);
if (TTF_Init() < 0)
{
print_sdl_error("Could not initialize SDL ttf");
flag_to_end();
}
else
{
printf("initialized SDL ttf %d.%d.%d\n", SDL_TTF_MAJOR_VERSION,
SDL_TTF_MINOR_VERSION, SDL_TTF_PATCHLEVEL);
}
if (Mix_Init(MIX_INIT_FLAC) == 0)
{
print_sdl_error("Could not initialize SDL mixer");
flag_to_end();
}
else
{
printf("initialized SDL mixer %d.%d.%d\n", SDL_MIXER_MAJOR_VERSION,
SDL_MIXER_MINOR_VERSION, SDL_MIXER_PATCHLEVEL);
}
if (Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT,
MIX_DEFAULT_CHANNELS, 1024) < 0)
{
print_sdl_error("Could not set up audio");
}
}
void Game::print_error(std::string message)
{
std::cerr << message << std::endl;
}
void Game::print_sdl_error(std::string message)
{
std::cerr << message << " " << SDL_GetError() << std::endl;
}
void Game::print_gl_attributes()
{
int major, minor;
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &major);
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &minor);
std::cout << "GL CONTEXT: " << major << ", " << minor << std::endl;
}
void Game::load_sdl_context()
{
if (glcontext != NULL)
{
SDL_GL_DeleteContext(glcontext);
glcontext = NULL;
}
if ((renderer = SDL_CreateRenderer(window, -1, 0)) == NULL)
{
print_sdl_error("Could not create renderer");
flag_to_end();
}
is_gl_context = false;
}
void Game::load_gl_context()
{
if (renderer != NULL)
{
SDL_DestroyRenderer(renderer);
renderer = NULL;
}
// SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
print_gl_attributes();
if ((glcontext = SDL_GL_CreateContext(window)) == NULL)
{
print_sdl_error("Could not get GL context");
flag_to_end();
}
GLenum error = glewInit();
if (error != GLEW_OK)
{
std::ostringstream message;
message << "GLEW could not initialize " << glewGetErrorString(error);
print_error(message.str());
}
printf("OpenGL %s, renderer %s, shading language %s\n", glGetString(GL_VERSION),
glGetString(GL_RENDERER), glGetString(GL_SHADING_LANGUAGE_VERSION));
is_gl_context = true;
}
void Game::run()
{
while (not done)
{
ticks = SDL_GetTicks();
if (ticks - last_frame_timestamp + frame_time_overflow >= frame_length)
{
last_frame_length = ticks - last_frame_timestamp;
frame_time_overflow = last_frame_length + frame_time_overflow - frame_length;
last_frame_timestamp = ticks;
update();
}
SDL_Delay(15);
}
}
// void Game::update()
// {
// }
void Game::flag_to_end()
{
done = true;
}
void Game::set_framerate(int fps)
{
if (fps < 1)
{
fps = 1;
}
framerate = fps;
frame_length = 1000.0 / framerate;
}
void Game::quit()
{
if (glcontext != NULL)
{
SDL_GL_DeleteContext(glcontext);
}
if (renderer != NULL)
{
SDL_DestroyRenderer(renderer);
}
if (window != NULL)
{
SDL_DestroyWindow(window);
}
if (TTF_WasInit())
{
TTF_Quit();
}
Mix_CloseAudio();
Mix_Quit();
SDL_Quit();
}

58
Game.hpp Normal file
View File

@ -0,0 +1,58 @@
#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
#include <glew.h>
#include "Node.hpp"
struct Game : Node
{
Game(const Game&) = delete;
Game& operator=(const Game&) = delete;
Game(Game&&) = delete;
Game& operator=(Game&&) = delete;
SDL_Window *window;
SDL_Renderer *renderer = NULL;
SDL_GLContext glcontext = NULL;
int sw = 640, sh = 480, framerate = 60, frame_time_overflow = 0,
last_frame_timestamp, frame_count_timestamp, ticks,
last_frame_length;
float frame_length = 1000.0 / framerate;
bool done = false, show_framerate = false, is_gl_context = true;
Game();
void print_error(std::string);
void print_sdl_error(std::string);
void print_gl_attributes();
void load_sdl_context();
void load_gl_context();
void run();
void flag_to_end();
virtual void update() = 0;
void set_framerate(int);
void quit();
template<typename T>
float get_weighted_amount(T amount)
{
return (last_frame_length / (1000.0 / 60)) * amount;
}
};
#endif

11
Location.cpp Normal file
View File

@ -0,0 +1,11 @@
#include "Location.hpp"
int Location::get_x()
{
return rect.x;
}
int Location::get_y()
{
return rect.y;
}

32
Location.hpp Normal file
View File

@ -0,0 +1,32 @@
#ifndef Location_h_
#define Location_h_
#include <SDL.h>
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/common.hpp>
#include <glm/vec2.hpp>
struct Location
{
SDL_Rect rect = {0, 0, 0, 0};
glm::vec2 overflow;
Location() { };
int get_x();
int get_y();
template<typename T1, typename T2>
void move_ip(T1 dx, T2 dy = 0)
{
overflow += glm::vec2(dx, dy);
glm::vec2 motion = glm::floor(overflow);
overflow -= motion;
rect.x += motion[0];
rect.y += motion[1];
}
};
#endif

View File

@ -36,10 +36,16 @@ glew.o : glew/glew.c
Location.o : Location.cpp Location.hpp
$(CPPC_LINUX) $(CFLAGS) $<
main.o : main.cpp main.hpp Location.hpp
Game.o : Game.cpp Game.hpp Node.hpp
$(CPPC_LINUX) $(CFLAGS) $<
linux : main.o Location.o glew.o SDL2_rotozoom.o SDL2_gfxPrimitives.o
Node.o : Node.cpp Node.hpp Game.hpp
$(CPPC_LINUX) $(CFLAGS) $<
main.o : main.cpp main.hpp Node.hpp Game.hpp Location.hpp
$(CPPC_LINUX) $(CFLAGS) $<
linux : main.o Node.o Game.o Location.o glew.o SDL2_rotozoom.o SDL2_gfxPrimitives.o
$(CPPC_LINUX) $(LFLAGS) -D__LINUX__ $^ -lGL -lSDL2_image -lSDL2_ttf -lSDL2_mixer -lstdc++fs -o main
android :

11
Node.cpp Normal file
View File

@ -0,0 +1,11 @@
#include "Node.hpp"
Game* Node::get_root()
{
Node *current = parent;
while (current->parent != NULL)
{
current = current->parent;
}
return static_cast<Game*>(current);
}

26
Node.hpp Normal file
View File

@ -0,0 +1,26 @@
#ifndef Node_h_
#define Node_h_
#include <iostream>
struct Game;
struct Node
{
Node *parent = NULL;
Node() { std::cout << "Default constructing Node with parent " << parent << std::endl; }
Node(Node *parent) : parent(parent)
{
std::cout << "Constructing Node with parent " << parent << std::endl;
}
Game *get_root();
};
#include "Game.hpp"
#endif

252
main.cpp
View File

@ -1,6 +1,8 @@
// reset, pause, auto reset
#include "main.hpp"
#include "Game.hpp"
#include "Node.hpp"
char* file_to_buf(const char *path)
{
@ -143,6 +145,7 @@ SDL_Surface* get_framerate_indicator_surface(int frame_count)
void set_framerate_indicator(int frame_count, GLuint id)
{
printf("%i %i", frame_count, id);
SDL_Surface *message = get_framerate_indicator_surface(frame_count);
glBindTexture(GL_TEXTURE_2D, id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, message->w, message->h, 0, GL_BGRA, GL_UNSIGNED_BYTE,
@ -151,23 +154,23 @@ void set_framerate_indicator(int frame_count, GLuint id)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
}
struct Game;
// struct Game;
struct Node
{
// struct Node
// {
Node *parent = NULL;
// Node *parent = NULL;
Node() { std::cout << "Default constructing Node with parent " << parent << std::endl; }
// Node() { std::cout << "Default constructing Node with parent " << parent << std::endl; }
Node(Node *parent) : parent(parent)
{
std::cout << "Constructing Node with parent " << parent << std::endl;
}
// Node(Node *parent) : parent(parent)
// {
// std::cout << "Constructing Node with parent " << parent << std::endl;
// }
Game *get_root();
// Game *get_root();
};
// };
struct Sprite : Node
{
@ -218,153 +221,42 @@ struct Mushroom : Sprite
};
struct Game : Node
struct Demo : Game
{
Game(const Game&) = delete;
Game& operator=(const Game&) = delete;
Game(Game&&) = delete;
Game& operator=(Game&&) = delete;
SDL_Window *window;
SDL_Renderer *renderer = NULL;
SDL_GLContext glcontext = NULL;
SDL_Texture *grass_texture;
int sw = 640, sh = 480, framerate = 60, recording_capture_framerate = 100,
frame_time_overflow = 0, capture_time_overflow = 0, frame_count = 0,
last_frame_timestamp, frame_count_timestamp, last_capture_timestamp, ticks, last_frame_length;
bool done = false, is_recording = false, show_framerate = false, is_gl_context = true,
right_active = false, down_active = false, left_active = false, up_active = false;
SDL_Event event;
float r = 0.0;
GLuint vbo, space_texture_id, mvp_id, framerate_texture_id, flat_program, world_program,
fake_texture_id;
float frame_length = 1000.0 / framerate;
int recording_capture_framerate = 100, frame_time_overflow = 0,
capture_time_overflow = 0, frame_count = 0, frame_count_timestamp,
last_capture_timestamp;
std::list<SDL_Surface*> frames;
bool is_recording = false, 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");
Game()
Demo() : Game()
{
std::cout << "GLEW " << glewGetString(GLEW_VERSION) << std::endl;
putenv("SDL_VIDEO_X11_LEGACY_FULLSCREEN=0");
SDL_version version;
SDL_GetVersion(&version);
printf("SDL %d.%d.%d\n", version.major, version.minor, version.patch);
fprintf(stderr, "stderr test message\n");
SDL_SetMainReady();
// SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
print_gl_attributes();
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
{
print_sdl_error("SDL could not initialize");
flag_to_end();
}
// SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
print_gl_attributes();
window = SDL_CreateWindow("TARE control", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
sw, sh, SDL_WINDOW_OPENGL);
if (window == NULL)
{
print_sdl_error("Could not create window");
flag_to_end();
}
SDL_ShowCursor(0);
if (TTF_Init() < 0)
{
print_sdl_error("Could not initialize SDL ttf");
flag_to_end();
}
else
{
printf("initialized SDL ttf %d.%d.%d\n", SDL_TTF_MAJOR_VERSION,
SDL_TTF_MINOR_VERSION, SDL_TTF_PATCHLEVEL);
}
if (Mix_Init(MIX_INIT_FLAC) == 0)
{
print_sdl_error("Could not initialize SDL mixer");
flag_to_end();
}
else
{
printf("initialized SDL mixer %d.%d.%d\n", SDL_MIXER_MAJOR_VERSION,
SDL_MIXER_MINOR_VERSION, SDL_MIXER_PATCHLEVEL);
}
if (Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT,
MIX_DEFAULT_CHANNELS, 1024) < 0)
{
print_sdl_error("Could not set up audio");
}
Mix_Music *music = Mix_LoadMUS("resource/Leper.mp3");
Mix_PlayMusic(music, -1);
load_gl_context();
}
void print_error(std::string message)
{
std::cerr << message << std::endl;
}
void print_sdl_error(std::string message)
{
std::cerr << message << " " << SDL_GetError() << std::endl;
}
void print_gl_attributes()
{
int major, minor;
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &major);
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &minor);
std::cout << "GL CONTEXT: " << major << ", " << minor << std::endl;
}
void load_sdl_context()
{
if (glcontext != NULL)
{
SDL_GL_DeleteContext(glcontext);
}
if ((renderer = SDL_CreateRenderer(window, -1, 0)) == NULL)
{
print_sdl_error("Could not create renderer");
flag_to_end();
}
Game::load_sdl_context();
grass.load();
mushroom.load();
is_gl_context = false;
}
void load_gl_context()
{
if (renderer != NULL)
{
SDL_DestroyRenderer(renderer);
}
Game::load_gl_context();
grass.unload();
mushroom.unload();
// SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
print_gl_attributes();
if ((glcontext = SDL_GL_CreateContext(window)) == NULL)
{
print_sdl_error("Could not get GL context");
flag_to_end();
}
GLenum error = glewInit();
if (error != GLEW_OK)
{
std::ostringstream message;
message << "GLEW could not initialize " << glewGetErrorString(error);
print_error(message.str());
}
printf("OpenGL %s, renderer %s, shading language %s\n", glGetString(GL_VERSION),
glGetString(GL_RENDERER), glGetString(GL_SHADING_LANGUAGE_VERSION));
/*
v0-v1-v2 (front)
v2-v3-v0
@ -503,41 +395,9 @@ struct Game : Node
glBindTexture(GL_TEXTURE_2D, space_texture_id);
glUniform1i(sampler_uniform_id, 0);
glDepthFunc(GL_LESS);
last_frame_timestamp = frame_count_timestamp = last_capture_timestamp = SDL_GetTicks();
frame_count_timestamp = last_capture_timestamp = SDL_GetTicks();
framerate_texture_id = get_gl_texture_from_surface(
get_framerate_indicator_surface(frame_count), GL_LINEAR);
is_gl_context = true;
}
void run()
{
while (not done)
{
ticks = SDL_GetTicks();
if (ticks - last_frame_timestamp + frame_time_overflow >= frame_length)
{
frame_count++;
last_frame_length = ticks - last_frame_timestamp;
frame_time_overflow = last_frame_length + frame_time_overflow - frame_length;
last_frame_timestamp = ticks;
update();
}
SDL_Delay(15);
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;
}
}
}
void flag_to_end()
{
done = true;
}
void update()
@ -715,56 +575,20 @@ struct Game : Node
mushroom.update();
SDL_RenderPresent(renderer);
}
}
void set_framerate(int fps)
{
framerate = fps;
frame_length = 1000.0 / framerate;
std::cout << frame_length << " " << framerate << std::endl;
}
template<typename T>
float get_weighted_amount(T amount)
{
return (last_frame_length / (1000.0 / 60)) * amount;
}
void quit()
{
if (glcontext != NULL)
frame_count++;
if (ticks - frame_count_timestamp >= 1000)
{
SDL_GL_DeleteContext(glcontext);
frame_count_timestamp = ticks;
if (is_gl_context and show_framerate)
{
set_framerate_indicator(frame_count, framerate_texture_id);
}
frame_count = 0;
}
if (renderer != NULL)
{
SDL_DestroyRenderer(renderer);
}
if (window != NULL)
{
SDL_DestroyWindow(window);
}
if (TTF_WasInit())
{
TTF_Quit();
}
Mix_CloseAudio();
Mix_Quit();
SDL_Quit();
}
};
Game* Node::get_root()
{
Node *current = parent;
while (current->parent != NULL)
{
current = current->parent;
}
return static_cast<Game*>(current);
}
void Sprite::associate(std::string path)
{
if (fs::is_regular_file(path))
@ -869,8 +693,8 @@ void Mushroom::update()
int main(int argc, char *argv[])
{
Game g;
g.run();
g.quit();
Demo demo;
demo.run();
demo.quit();
return 0;
}

41
main.hpp Normal file
View File

@ -0,0 +1,41 @@
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <array>
#include <list>
#include <cstdlib>
#include <algorithm>
#include <string>
#if defined(__MINGW32__)
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#else
#include <filesystem>
namespace fs = std::filesystem;
#endif
#include <SDL_image.h>
#include <SDL2_gfxPrimitives.h>
// #if defined(__LINUX__) or defined(__MINGW32__)
// #define GL_GLEXT_PROTOTYPES
// #include <GL/gl.h>
// #elif defined(__ANDROID__)
// #include <GLES/gl.h>
// #elif defined(__EMSCRIPTEN__)
// #include <GLES2/gl2.h>
// #elif defined(__APPLE__)
// #include <OpenGL/gl3.h>
// #endif
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/string_cast.hpp>
#include <glm/gtx/transform.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <SDL2_rotozoom.h>
#include "Game.hpp"
#include "Node.hpp"
#include "Location.hpp"