mirror of
https://github.com/haya3218/SDfmL.git
synced 2024-08-14 23:57:09 +00:00
Timers. This took a bit to figure out.
Timer code fucking sucks someone pls improve
This commit is contained in:
parent
2120f83e1c
commit
5015500748
3 changed files with 77 additions and 18 deletions
22
src/Main.cpp
22
src/Main.cpp
|
@ -107,7 +107,6 @@ MainState m2;
|
||||||
class SplashScreen : public State {
|
class SplashScreen : public State {
|
||||||
Object bg;
|
Object bg;
|
||||||
Object black;
|
Object black;
|
||||||
int tick[2];
|
|
||||||
virtual void Create() {
|
virtual void Create() {
|
||||||
bg.create(0, 0, "data/powered.png");
|
bg.create(0, 0, "data/powered.png");
|
||||||
AddObject(&bg);
|
AddObject(&bg);
|
||||||
|
@ -117,26 +116,15 @@ class SplashScreen : public State {
|
||||||
|
|
||||||
black.alpha = 0;
|
black.alpha = 0;
|
||||||
|
|
||||||
|
Timer time;
|
||||||
|
Timer time2;
|
||||||
|
time2.start(1.0, [this](int dummy) {black.alpha += 1; return 0;}, true);
|
||||||
|
time.start(3.0, [](int dummy) {SwitchState(&m2); return 0;});
|
||||||
|
|
||||||
playSound("data/flixel.ogg");
|
playSound("data/flixel.ogg");
|
||||||
|
|
||||||
// basic timer
|
|
||||||
tick[0] = Sec2Tick(1.0);
|
|
||||||
tick[1] = Sec2Tick(3.0);
|
|
||||||
|
|
||||||
//SDL_SetRenderDrawColor( renderer, 0xFF, 0xFF, 0xFF, 0xFF );
|
//SDL_SetRenderDrawColor( renderer, 0xFF, 0xFF, 0xFF, 0xFF );
|
||||||
}
|
}
|
||||||
int now[2] = {0, 0};
|
|
||||||
virtual void Update(float dt) {
|
|
||||||
for (int i = 0; i < 2; i++) {
|
|
||||||
now[i]++;
|
|
||||||
}
|
|
||||||
if (!(now[0] < tick[0])) {
|
|
||||||
black.alpha += 1;
|
|
||||||
}
|
|
||||||
if (!(now[1] < tick[1])) {
|
|
||||||
SwitchState(&m2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
SplashScreen m;
|
SplashScreen m;
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
|
#include <functional>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <iterator>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
@ -87,6 +89,9 @@ void Render::Object::Draw(float dt) {
|
||||||
if (alpha > 100)
|
if (alpha > 100)
|
||||||
alpha = 100;
|
alpha = 100;
|
||||||
|
|
||||||
|
if (alpha < 0)
|
||||||
|
alpha = 0;
|
||||||
|
|
||||||
SDL_SetTextureAlphaMod(this->_tex, (this->alpha/100)*255);
|
SDL_SetTextureAlphaMod(this->_tex, (this->alpha/100)*255);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -152,6 +157,7 @@ void Render::TextObject::Draw(float dt) {
|
||||||
eff.color = color;
|
eff.color = color;
|
||||||
eff.scale.x = scale.x;
|
eff.scale.x = scale.x;
|
||||||
eff.scale.y = scale.y;
|
eff.scale.y = scale.y;
|
||||||
|
FC_SetFilterMode(font, (antialiasing ? FC_FILTER_LINEAR : FC_FILTER_NEAREST));
|
||||||
FC_DrawEffect(font, renderer, x-offset.x, y-offset.y, eff, text.c_str());
|
FC_DrawEffect(font, renderer, x-offset.x, y-offset.y, eff, text.c_str());
|
||||||
|
|
||||||
SDL_Rect rec = FC_GetBounds(font, x-offset.x, y-offset.y, alignment, eff.scale, text.c_str());
|
SDL_Rect rec = FC_GetBounds(font, x-offset.x, y-offset.y, alignment, eff.scale, text.c_str());
|
||||||
|
@ -221,6 +227,19 @@ bool Render::Init(string window_name) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
vector<float> Render::_sec;
|
||||||
|
vector<Func<int>> Render::_call;
|
||||||
|
vector<bool> Render::_repeats;
|
||||||
|
vector<int> Render::_ticks;
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void remove_a(std::vector<T>& vec, size_t pos)
|
||||||
|
{
|
||||||
|
typename std::vector<T>::iterator it = vec.begin();
|
||||||
|
std::advance(it, pos);
|
||||||
|
vec.erase(it);
|
||||||
|
}
|
||||||
|
|
||||||
bool Render::Update() {
|
bool Render::Update() {
|
||||||
int lastUpdate = SDL_GetTicks();
|
int lastUpdate = SDL_GetTicks();
|
||||||
bool run = true;
|
bool run = true;
|
||||||
|
@ -241,6 +260,32 @@ bool Render::Update() {
|
||||||
|
|
||||||
current_state->Update(dT);
|
current_state->Update(dT);
|
||||||
|
|
||||||
|
// I know this is a shitty way to do this
|
||||||
|
// but bear with me
|
||||||
|
// it was hard to work with lambdas properly man
|
||||||
|
// let me have this just one please :)
|
||||||
|
if (_sec.size() > 0) {
|
||||||
|
for (int i = 0; i < _sec.size(); i++) {
|
||||||
|
if (_ticks[i] != -1)
|
||||||
|
_ticks[i]++;
|
||||||
|
|
||||||
|
if (_sec[i] != -1) {
|
||||||
|
if (!(_ticks[i] < Sec2Tick(_sec[i]))) {
|
||||||
|
if (_call[i] != NULL)
|
||||||
|
_call[i](NULL);
|
||||||
|
if (!_repeats[i] && _repeats[i] != NULL)
|
||||||
|
{
|
||||||
|
_ticks[i] = -1;
|
||||||
|
_sec[i] = -1;
|
||||||
|
_call[i] = NULL;
|
||||||
|
_repeats[i] = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// cout << i << endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
lastUpdate = current;
|
lastUpdate = current;
|
||||||
|
|
||||||
current_state->Draw(dT);
|
current_state->Draw(dT);
|
||||||
|
@ -266,6 +311,10 @@ bool Render::Update() {
|
||||||
|
|
||||||
void Render::SwitchState(State* state) {
|
void Render::SwitchState(State* state) {
|
||||||
if (current_state != nullptr) {
|
if (current_state != nullptr) {
|
||||||
|
_ticks = {};
|
||||||
|
_call = {};
|
||||||
|
_repeats = {};
|
||||||
|
_sec = {};
|
||||||
for (int i = 0; i < current_state->get_obj().size(); i++) {
|
for (int i = 0; i < current_state->get_obj().size(); i++) {
|
||||||
SDL_DestroyTexture(current_state->get_obj()[i]->_tex);
|
SDL_DestroyTexture(current_state->get_obj()[i]->_tex);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
#ifndef _RENDER_H
|
#ifndef _RENDER_H
|
||||||
#define _RENDER_H
|
#define _RENDER_H
|
||||||
#include <array>
|
#include <array>
|
||||||
|
#include <functional>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
#include <stdint.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
|
@ -132,10 +134,29 @@ namespace Render {
|
||||||
FC_AlignEnum alignment = FC_ALIGN_LEFT;
|
FC_AlignEnum alignment = FC_ALIGN_LEFT;
|
||||||
SDL_Color color;
|
SDL_Color color;
|
||||||
string text = "";
|
string text = "";
|
||||||
|
bool antialiasing = false;
|
||||||
private:
|
private:
|
||||||
int font_size = 20;
|
int font_size = 20;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
using Func = std::function<T(T)>;
|
||||||
|
|
||||||
|
extern vector<float> _sec;
|
||||||
|
extern vector<Func<int>> _call;
|
||||||
|
extern vector<bool> _repeats;
|
||||||
|
extern vector<int> _ticks;
|
||||||
|
|
||||||
|
class Timer {
|
||||||
|
public:
|
||||||
|
void start(float seconds, Func<int> callback, bool repeat = false) {
|
||||||
|
_sec.push_back(seconds);
|
||||||
|
_call.push_back(callback);
|
||||||
|
_repeats.push_back(repeat);
|
||||||
|
_ticks.push_back(0);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* A state is where you would contain said objects.
|
* A state is where you would contain said objects.
|
||||||
*/
|
*/
|
||||||
|
@ -195,11 +216,12 @@ namespace Render {
|
||||||
void SwitchState(State* state);
|
void SwitchState(State* state);
|
||||||
|
|
||||||
extern SDL_Event event;
|
extern SDL_Event event;
|
||||||
extern State* current_state;
|
|
||||||
|
|
||||||
extern HWND hwnd;
|
extern HWND hwnd;
|
||||||
extern HWND consoleD;
|
extern HWND consoleD;
|
||||||
|
|
||||||
|
extern State* current_state;
|
||||||
|
|
||||||
extern array<anshub::AudioOut, MAX_SE> audioArray;
|
extern array<anshub::AudioOut, MAX_SE> audioArray;
|
||||||
extern anshub::AudioOut music;
|
extern anshub::AudioOut music;
|
||||||
extern string currentMusic;
|
extern string currentMusic;
|
||||||
|
|
Loading…
Reference in a new issue