display sprite frames

This commit is contained in:
Frank DeMarco 2018-12-27 05:01:43 -05:00
parent 2d9a42bdba
commit 8cd25fc099
11 changed files with 110 additions and 24 deletions

View File

@ -17,8 +17,8 @@ SYSFWPATH = /Library/Frameworks
INC = -Iglm -Isdl2-gfx
linux :
clang++-7 -c $(CFLAGS) $(INC) -D__LINUX__ main.cpp sdl2-gfx/SDL2_gfxPrimitives.c sdl2-gfx/SDL2_rotozoom.c
clang++-7 $(LFLAGS) SDL2_gfxPrimitives.o SDL2_rotozoom.o main.o -lSDL2_image -lSDL2_ttf -lGL -o main
clang++-7 -std=c++17 -c $(CFLAGS) $(INC) -D__LINUX__ main.cpp sdl2-gfx/SDL2_gfxPrimitives.c sdl2-gfx/SDL2_rotozoom.c
clang++-7 $(LFLAGS) SDL2_gfxPrimitives.o SDL2_rotozoom.o main.o -lSDL2_image -lSDL2_ttf -lGL -lstdc++fs -o main
android :
if [ ! -d $(BUILDDIR) ]; then mkdir $(BUILDDIR); fi;

130
main.cpp
View File

@ -188,36 +188,43 @@ struct Node
Node *parent = NULL;
Node() { std::cout << "Default constructing Node..." << 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;
}
Game *get_root()
{
Node *current = parent;
while (current->parent != NULL)
{
current = current->parent;
}
return (Game*) (current);
}
Game *get_root();
};
struct Sprite : Node
{
SDL_Texture *texture;
SDL_Rect rect;
std::vector<SDL_Texture*> frames;
int frame_ii = 0;
SDL_Rect rect = {0, 0, 16, 16};
Sprite(Node *parent) : Node(parent)
{
std::cout << "Constructing Sprite with parent " << parent << std::endl;
}
Sprite(Node *parent, std::string path) : Node(parent)
{
std::cout << "Constructing Sprite with " << parent << " and " << path << std::endl;
load(path);
}
void load(std::string);
void load_file(std::string);
void add_frame(SDL_Texture*);
void update();
};
struct Mushroom : Sprite
@ -233,6 +240,11 @@ struct Mushroom : Sprite
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;
@ -248,12 +260,10 @@ struct Game : Node
float frame_length = 1000.0 / framerate;
std::list<SDL_Surface*> frames;
glm::mat4 projection, view, model = glm::mat4(1.0f), mvp;
Mushroom mushroom = Mushroom(this);
Game()
{
Mushroom mushroom(this);
Game *root = mushroom.get_root();
std::cout << root->sw << std::endl;
setenv("SDL_VIDEO_X11_LEGACY_FULLSCREEN", "0", true);
SDL_version version;
SDL_GetVersion(&version);
@ -263,7 +273,7 @@ struct Game : Node
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
print_sdl_error("SDL could not initialize");
done = true;
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);
@ -273,17 +283,22 @@ struct Game : Node
if (window == NULL)
{
print_sdl_error("Could not create window");
done = true;
flag_to_end();
}
SDL_ShowCursor(0);
if (TTF_Init() < 0)
{
print_sdl_error("Could not initialize SDL ttf");
done = true;
flag_to_end();
}
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;
@ -298,13 +313,14 @@ struct Game : Node
if ((renderer = SDL_CreateRenderer(window, -1, 0)) == NULL)
{
print_sdl_error("Could not create renderer");
done = true;
flag_to_end();
}
if (!(grass_texture = IMG_LoadTexture(renderer, "resource/Field.png")))
{
print_sdl_error("Could not load image");
done = true;
flag_to_end();
}
mushroom.load("resource/shrooms");
is_gl_context = false;
}
@ -317,7 +333,7 @@ struct Game : Node
if ((glcontext = SDL_GL_CreateContext(window)) == NULL)
{
print_sdl_error("Could not get GL context");
done = true;
flag_to_end();
}
printf("OpenGL %s, renderer %s, shading language %s\n", glGetString(GL_VERSION),
glGetString(GL_RENDERER), glGetString(GL_SHADING_LANGUAGE_VERSION));
@ -490,13 +506,18 @@ struct Game : Node
}
}
void flag_to_end()
{
done = true;
}
void update()
{
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
done = true;
flag_to_end();
}
else if (event.type == SDL_KEYDOWN)
{
@ -600,6 +621,7 @@ struct Game : Node
SDL_RenderCopy(renderer, grass_texture, NULL, &rect);
roundedBoxColor(renderer, 300, 200, 500, 300, 10, 0x8f8fdfff);
aacircleColor(renderer, 300, 200, 30, 0xffef3fff);
mushroom.update();
SDL_RenderPresent(renderer);
}
}
@ -627,6 +649,70 @@ struct Game : Node
};
Game* Node::get_root()
{
Node *current = parent;
while (current->parent != NULL)
{
current = current->parent;
}
return static_cast<Game*>(current);
}
void Sprite::load(std::string path)
{
Game *game = get_root();
if (std::filesystem::is_regular_file(path))
{
load_file(path);
}
else if (std::filesystem::is_directory(path))
{
std::filesystem::directory_iterator directory(path);
std::vector<std::filesystem::path> paths;
std::copy(directory, std::filesystem::directory_iterator(), std::back_inserter(paths));
std::sort(paths.begin(), paths.end());
for (const std::filesystem::path &name : paths)
{
std::cout << name << std::endl;
load_file(name);
}
}
else
{
std::ostringstream message;
message << "invalid path " << path;
game->print_error(message.str());
}
}
void Sprite::load_file(std::string path)
{
Game *game = get_root();
SDL_Texture *texture = IMG_LoadTexture(game->renderer, path.c_str());
if (!texture)
{
game->print_sdl_error("Could not load image");
}
else
{
add_frame(texture);
}
}
void Sprite::add_frame(SDL_Texture *texture)
{
frames.push_back(texture);
}
void Sprite::update()
{
SDL_RenderCopy(get_root()->renderer, frames[frame_ii++], NULL, &rect);
if (frame_ii >= frames.size())
{
frame_ii = 0;
}
}
int main(int argc, char *argv[])
{

Binary file not shown.

After

Width:  |  Height:  |  Size: 282 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 274 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 258 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 282 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 251 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 307 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 289 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 281 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 270 B