Added more documentation and make AnimatedObject support scaling

This commit is contained in:
/nick haya 2022-02-07 15:59:38 +08:00
parent b3cca999d7
commit e7d3c8a3b9
4 changed files with 60 additions and 3 deletions

BIN
bin/data/monogram.ttf Normal file

Binary file not shown.

View file

@ -11,6 +11,7 @@ using namespace Render;
class MainState : public State {
AnimatedObject objs;
TextObject text;
Object title;
virtual void Create() {
title.create(0, 0, "data/bg.png");
@ -24,6 +25,11 @@ class MainState : public State {
title.center.x = WINDOW_WIDTH/2;
title.center.y = WINDOW_HEIGHT/2;
text.create(50, 480 - 100, "funny");
text.scale.x = 2;
text.scale.y = 2;
AddObject(&text);
}
int yvel = 0;
int xvel = 0;
@ -46,7 +52,7 @@ class MainState : public State {
};
int main() {
Init("lmfao");
Init("SDLflixel :)))))");
MainState m;

View file

@ -114,6 +114,40 @@ void Render::AnimatedObject::PlayAnimation(string anim_name) {
current_framename = anim_name;
}
void Render::TextObject::create(int x, int y, string text, string font_name) {
Render::Object::create(x,y,"data/smile.png"); // dummy
font = TTF_OpenFont(font_name.c_str(), 25);
SDL_Surface* temp = TTF_RenderText_Solid(font, text.c_str(), {255, 255, 255, 255});
this->text = text;
this->_tex = SDL_CreateTextureFromSurface(renderer, temp);
SDL_FreeSurface(temp);
int w_, h_;
SDL_QueryTexture(this->_tex, NULL, NULL, &w_, &h_);
_sc_w = w_;
_sc_h = h_;
this->x = x;
this->y = y;
w = w_;
h = h_;
}
void Render::TextObject::changeText(string text) {
SDL_Surface* temp = TTF_RenderText_Solid(font, text.c_str(), {255, 255, 255, 255});
this->text = text;
this->_tex = SDL_CreateTextureFromSurface(renderer, temp);
SDL_FreeSurface(temp);
int w_, h_;
SDL_QueryTexture(this->_tex, NULL, NULL, &w_, &h_);
_sc_w = w_;
_sc_h = h_;
w = w_;
h = h_;
}
void Render::Object::centerSelf(AXIS axis) {
switch (axis) {
case X:

View file

@ -7,6 +7,7 @@
#include <string>
#include <windows.h>
#include "SDL2/SDL.h"
#include "SDL2/SDL_ttf.h"
#include "BASS/audio_out.h"
using namespace std;
@ -47,7 +48,7 @@ namespace Render {
/*
* Create a new Object instance.
*/
void create(int x = 0, int y = 0, string path = "");
virtual void create(int x = 0, int y = 0, string path = "");
virtual void Draw(float dt);
@ -84,7 +85,7 @@ namespace Render {
/*
* Create a new AnimatedObject instance.
*/
void create(int x = 0, int y = 0, string path = "");
virtual void create(int x = 0, int y = 0, string path = "");
/*
* Add an animation to said object. Uses SDL_Rects for frames.
*/
@ -104,6 +105,22 @@ namespace Render {
int startTime;
};
class TextObject : public Object {
public:
/*
* Create a new TextObject instance.
*/
virtual void create(int x = 0, int y = 0, string text = "", string font_name = "data/monogram.ttf");
/*
* Change current text. !! TO AVOID MEMORY LEAKS, DO NOT RUN THIS EVERY FRAME! !!
*/
virtual void changeText(string text = "");
TTF_Font* font;
private:
string text = "";
};
/*
* A state is where you would contain said objects.
*/