aminmations

This commit is contained in:
2024-09-06 10:34:29 +01:00
parent a756a4f68e
commit 8091651cc7
12 changed files with 421 additions and 0 deletions

20
src/input.cpp Normal file
View File

@@ -0,0 +1,20 @@
#include "input.h"
void Input::update()
{
while (SDL_PollEvent(&event))
{
if (event.type == SDL_KEYDOWN)
{
activeKeys[event.key.keysym.sym] = true;
}
else if (event.type == SDL_KEYUP)
{
activeKeys.erase(event.key.keysym.sym);
}
else if (event.type == SDL_QUIT)
{
exit = true;
}
}
}

95
src/main.cpp Normal file
View File

@@ -0,0 +1,95 @@
// Inludes
#include <SDL.h>
#include <string>
#include <vector>
#include "input.h"
#include "text.h"
#include "sprite.h"
// Variables
SDL_Surface* assets;
int startTime = 0;
Input input;
TTF_Font *font;
SDL_Window *window;
SDL_Renderer *renderer;
const int targetFps = 60;
// Functions
// Quit function
void quit()
{
// Free assets
TTF_CloseFont(font);
SDL_FreeSurface(assets);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
TTF_Quit();
// Exit program
exit(0);
}
// Main function
int main(int argc, char* argv[])
{
// Initialize SDL and TFF;
TTF_Init();
SDL_Init(SDL_INIT_VIDEO);
// Create window and renderer
window = SDL_CreateWindow("test game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 600, 400, SDL_WINDOW_SHOWN);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
// Load assets
assets = SDL_LoadBMP("assets/sprites.bmp");
// frame map
std::map<std::string, SDL_Rect> frames = {
{ "idle", { 4, 456, 68, 125 } },
{ "idle2", { 76, 456, 68, 128 } },
{ "idle3", { 148, 456, 68, 125 } }
};
// Create sprite and animation
Sprite* sprite = new Sprite(renderer, assets);
AnimationManager* animations = new AnimationManager(sprite);
animations->addAnimation("idle", frames);
animations->setAnimation("idle");
// Main loop
while (true)
{
// Start time
startTime = SDL_GetTicks();
// Update input
input.update();
// Exit if window is closed
if (input.exit)
{
quit();
}
// Update animation
animations->update();
// Clear screen
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
// Draw sprite
animations->draw(renderer);
// Update screen
SDL_RenderPresent(renderer);
// if frame is faster than target fps, delay
if (1000 / targetFps > SDL_GetTicks() - startTime)
{
SDL_Delay(1000 / targetFps - (SDL_GetTicks() - startTime));
}
}
return 0;
}

126
src/sprite.cpp Normal file
View File

@@ -0,0 +1,126 @@
#include "sprite.h"
Sprite::Sprite(SDL_Renderer *renderer, SDL_Surface *surface)
{
Texture = SDL_CreateTextureFromSurface(renderer, surface);
dstrect = { 0, 0, surface->w, surface->h };
}
Sprite::~Sprite()
{
SDL_DestroyTexture(Texture);
}
void Sprite::draw(SDL_Renderer* renderer, std::string frame, bool autoRect, bool flip)
{
if (frames.find(frame) == frames.end())
{
printf("Frame not found\n");
return;
}
if (autoRect)
{
dstrect = { dstrect.x, dstrect.y, frames[frame].w, frames[frame].h };
}
SDL_SetTextureAlphaMod(Texture, 255);
dstrect.w *= scale.x;
dstrect.h *= scale.y;
SDL_RenderCopyEx(renderer, Texture, &frames[frame], &dstrect, angle, NULL, flip ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE);
}
void Sprite::addFrame(std::string name, SDL_Rect rect)
{
frames[name] = rect;
}
void Sprite::move(int_vec2 position)
{
dstrect = { position.x, position.y, dstrect.w, dstrect.h };
}
int_vec2 Sprite::getSize(std::string frame)
{
size = { frames[frame].w, frames[frame].h };
return size;
}
void Sprite::autorect(std::string frame)
{
dstrect = { dstrect.x, dstrect.y, frames[frame].w, frames[frame].h };
}
Animation::Animation(std::map<std::string, SDL_Rect> frames, Sprite* sprite, int frameDuration)
{
this->sprite = sprite;
this->frameDuration = frameDuration;
for (auto const& x : frames)
{
this->frames.push_back(x.first);
this->sprite->addFrame(x.first, x.second);
}
currentFrameName = this->frames[0];
}
void Animation::update()
{
frameCounter++;
if (frameCounter >= frameDuration)
{
frameCounter = 0;
if (pingpong)
{
frameIndex += frameDirection;
if (frameIndex >= frames.size() - 1 || frameIndex <= 0)
{
frameDirection *= -1;
}
}
else
{
frameIndex++;
if (frameIndex >= frames.size())
{
frameIndex = 0;
}
}
currentFrameName = frames[frameIndex];
}
}
void Animation::draw(SDL_Renderer* renderer, bool autoRect, bool flip)
{
sprite->draw(renderer, currentFrameName, autoRect, flip);
}
AnimationManager::AnimationManager(Sprite* sprite)
{
this->sprite = sprite;
}
void AnimationManager::addAnimation(std::string name, std::map<std::string, SDL_Rect> frames, int frameDuration, bool flip, bool autoRect, bool loop, bool pingpong)
{
animations[name] = new Animation(frames, sprite, frameDuration);
this->flip[name] = flip;
this->autoRect[name] = autoRect;
animations[name].loop = loop;
animations[name].pingpong = pingpong;
}
void AnimationManager::setAnimation(std::string name)
{
currentAnimation = name;
}
void AnimationManager::update()
{
animations[currentAnimation].update();
}
void AnimationManager::draw(SDL_Renderer* renderer)
{
animations[currentAnimation].draw(renderer, autoRect[currentAnimation], flip[currentAnimation]);
}

17
src/text.cpp Normal file
View File

@@ -0,0 +1,17 @@
#include "text.h"
Text::Text(TTF_Font* font, SDL_Color color)
{
this->font = font;
this->color = color;
}
void Text::draw(SDL_Renderer* renderer, std::string text, int x, int y)
{
surface = TTF_RenderText_Solid(font, text.c_str(), color);
texture = SDL_CreateTextureFromSurface(renderer, surface);
rect = { x, y, surface->w, surface->h };
SDL_RenderCopy(renderer, texture, NULL, &rect);
SDL_FreeSurface(surface);
SDL_DestroyTexture(texture);
}