mirror of
https://github.com/haya3218/SDfmL.git
synced 2024-08-14 23:57:09 +00:00
transitions
currently buggy tho
This commit is contained in:
parent
5d0e099482
commit
a1aacff687
5 changed files with 116 additions and 37 deletions
|
@ -1,5 +1,5 @@
|
||||||
# Make sur cmake is 3.8
|
# Make sur cmake is 3.10
|
||||||
cmake_minimum_required(VERSION 3.8)
|
cmake_minimum_required(VERSION 3.10)
|
||||||
|
|
||||||
# proj name
|
# proj name
|
||||||
project(Skateboard)
|
project(Skateboard)
|
||||||
|
@ -14,16 +14,16 @@ set(OUT5_INCLUDE_DIRS "${CMAKE_CURRENT_LIST_DIR}/src/SDL_gpu/")
|
||||||
|
|
||||||
option(PREFER_MODPLUG "Use libmodplug api instead of libopenmpt for module playback" ON)
|
option(PREFER_MODPLUG "Use libmodplug api instead of libopenmpt for module playback" ON)
|
||||||
|
|
||||||
|
if (${CMAKE_SIZEOF_VOID_P} MATCHES 8)
|
||||||
|
set(MAIN_LIBRARIES_E "${CMAKE_CURRENT_LIST_DIR}/lib/x64")
|
||||||
|
else ()
|
||||||
|
set(MAIN_LIBRARIES_E "${CMAKE_CURRENT_LIST_DIR}/lib/x86")
|
||||||
|
endif ()
|
||||||
|
|
||||||
# Support both 32 and 64 bit builds
|
# Support both 32 and 64 bit builds
|
||||||
# Someone make a pull request to support MingW
|
# Someone make a pull request to support MingW
|
||||||
# Way to lazy to do that right now
|
# Way to lazy to do that right now
|
||||||
if (${CMAKE_SIZEOF_VOID_P} MATCHES 8)
|
set(MAIN_LIBRARIES "${MAIN_LIBRARIES_E}/SDL2_gpu.lib;${MAIN_LIBRARIES_E}/libmodplug.lib;${MAIN_LIBRARIES_E}/soloud_static.lib;${MAIN_LIBRARIES_E}/SDL2.lib;${MAIN_LIBRARIES_E}/SDL2_ttf.lib")
|
||||||
set(MAIN_LIBRARIES "${CMAKE_CURRENT_LIST_DIR}/lib/x64/SDL2_gpu.lib;${CMAKE_CURRENT_LIST_DIR}/lib/x64/libmodplug.lib;${CMAKE_CURRENT_LIST_DIR}/lib/x64/soloud_static.lib;${CMAKE_CURRENT_LIST_DIR}/lib/x64/SDL2.lib;${CMAKE_CURRENT_LIST_DIR}/lib/x64/SDL2_ttf.lib")
|
|
||||||
set(MAIN_LIBRARIES_E "${CMAKE_CURRENT_LIST_DIR}/lib/x64/")
|
|
||||||
else ()
|
|
||||||
set(MAIN_LIBRARIES "${CMAKE_CURRENT_LIST_DIR}/lib/x86/SDL2_gpu.lib;${CMAKE_CURRENT_LIST_DIR}/lib/x86/libmodplug.lib;${CMAKE_CURRENT_LIST_DIR}/lib/x86/soloud_static.lib;${CMAKE_CURRENT_LIST_DIR}/lib/x86/SDL2.lib;${CMAKE_CURRENT_LIST_DIR}/lib/x86/SDL2_ttf.lib")
|
|
||||||
set(MAIN_LIBRARIES_E "${CMAKE_CURRENT_LIST_DIR}/lib/x86/")
|
|
||||||
endif ()
|
|
||||||
|
|
||||||
# strip it all
|
# strip it all
|
||||||
string(STRIP "${MAIN_LIBRARIES}" MAIN_LIBRARIES)
|
string(STRIP "${MAIN_LIBRARIES}" MAIN_LIBRARIES)
|
||||||
|
@ -80,7 +80,7 @@ set_target_properties(Skateboard PROPERTIES
|
||||||
)
|
)
|
||||||
|
|
||||||
# copy dll files
|
# copy dll files
|
||||||
file(COPY ${MAIN_LIBRARIES_E} DESTINATION ${BUILD_DIRECTORY})
|
file(COPY "${MAIN_LIBRARIES_E}/" DESTINATION ${BUILD_DIRECTORY})
|
||||||
|
|
||||||
# remove lib files
|
# remove lib files
|
||||||
file(REMOVE_RECURSE "${BUILD_DIRECTORY}/*.lib")
|
file(REMOVE_RECURSE "${BUILD_DIRECTORY}/*.lib")
|
||||||
|
|
|
@ -28,6 +28,7 @@ To use it (in windows atleast),
|
||||||
|
|
||||||
- Get SDL2 and SDL_ttf 2.0, and put em on the respective x86 and x64 folders.
|
- Get SDL2 and SDL_ttf 2.0, and put em on the respective x86 and x64 folders.
|
||||||
You know how to place them. It's fucking common sense.
|
You know how to place them. It's fucking common sense.
|
||||||
|
- Get SDL_gpu as well.
|
||||||
- [SoLoud fork.](https://github.com/haya3218/soloud) Follow GENie instructions and build as a static library.
|
- [SoLoud fork.](https://github.com/haya3218/soloud) Follow GENie instructions and build as a static library.
|
||||||
- You also need to build libmodplug with it as well.
|
- You also need to build libmodplug with it as well.
|
||||||
- If you are gonna be using the OpenMPT module, you'll need the libopenmpt dll in your exe directory as well.
|
- If you are gonna be using the OpenMPT module, you'll need the libopenmpt dll in your exe directory as well.
|
||||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 339 B After Width: | Height: | Size: 12 KiB |
|
@ -39,6 +39,7 @@ class ExampleState : public sdfml::sdState {
|
||||||
add(&bg1);
|
add(&bg1);
|
||||||
bg1.updateCamera(&camera);
|
bg1.updateCamera(&camera);
|
||||||
add(&example);
|
add(&example);
|
||||||
|
//bg1.screenCenter();
|
||||||
}
|
}
|
||||||
virtual void update(float elapsed) {
|
virtual void update(float elapsed) {
|
||||||
if (sdfml::key_pressed(SDL_SCANCODE_LEFT))
|
if (sdfml::key_pressed(SDL_SCANCODE_LEFT))
|
||||||
|
@ -49,9 +50,12 @@ class ExampleState : public sdfml::sdState {
|
||||||
example.y -= 1;
|
example.y -= 1;
|
||||||
if (sdfml::key_pressed(SDL_SCANCODE_DOWN))
|
if (sdfml::key_pressed(SDL_SCANCODE_DOWN))
|
||||||
example.y += 1;
|
example.y += 1;
|
||||||
|
if (sdfml::key_just_pressed(SDL_SCANCODE_R))
|
||||||
|
sdfml::switchState(this);
|
||||||
|
|
||||||
bg1.scale.x = sin(sdfml::elapsed/100);
|
bg1.scale.x = sin(sdfml::elapsed/100);
|
||||||
bg1.scale.y = cos(sdfml::elapsed/100);
|
bg1.scale.y = cos(sdfml::elapsed/100);
|
||||||
|
|
||||||
bg1.screenCenter();
|
bg1.screenCenter();
|
||||||
|
|
||||||
sdfml::focusCamera(&camera, example);
|
sdfml::focusCamera(&camera, example);
|
||||||
|
|
|
@ -38,6 +38,9 @@
|
||||||
|
|
||||||
#include "SDL_gpu/SDL_gpu.h"
|
#include "SDL_gpu/SDL_gpu.h"
|
||||||
|
|
||||||
|
#include <thread> // std::this_thread::sleep_for
|
||||||
|
#include <chrono> // std::chrono::seconds
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#define __FILENAME__ (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__)
|
#define __FILENAME__ (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__)
|
||||||
#else
|
#else
|
||||||
|
@ -65,6 +68,13 @@ struct Vector2
|
||||||
int y = 0;
|
int y = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct Vector3 {
|
||||||
|
T r;
|
||||||
|
T g;
|
||||||
|
T b;
|
||||||
|
};
|
||||||
|
|
||||||
enum lLOG_TYPE {
|
enum lLOG_TYPE {
|
||||||
NORMAL,
|
NORMAL,
|
||||||
WARNING,
|
WARNING,
|
||||||
|
@ -147,6 +157,25 @@ namespace sdfml {
|
||||||
|
|
||||||
static context mContext;
|
static context mContext;
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline int getIndex(vector<T> v, T K)
|
||||||
|
{
|
||||||
|
auto it = find(v.begin(), v.end(), K);
|
||||||
|
|
||||||
|
// If element was found
|
||||||
|
if (it != v.end())
|
||||||
|
{
|
||||||
|
|
||||||
|
// calculating the index
|
||||||
|
// of K
|
||||||
|
int index = it - v.begin();
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class sdSprite {
|
class sdSprite {
|
||||||
public:
|
public:
|
||||||
int x, y, width, height;
|
int x, y, width, height;
|
||||||
|
@ -156,6 +185,8 @@ namespace sdfml {
|
||||||
Vector2f scale = {1, 1};
|
Vector2f scale = {1, 1};
|
||||||
Vector2 offset;
|
Vector2 offset;
|
||||||
|
|
||||||
|
Vector3<Uint8> color;
|
||||||
|
|
||||||
virtual void create(int x, int y, string path) {
|
virtual void create(int x, int y, string path) {
|
||||||
this->x = x;
|
this->x = x;
|
||||||
this->y = y;
|
this->y = y;
|
||||||
|
@ -163,34 +194,26 @@ namespace sdfml {
|
||||||
GPU_SetBlendMode(_tex_gpu, GPU_BLEND_NORMAL);
|
GPU_SetBlendMode(_tex_gpu, GPU_BLEND_NORMAL);
|
||||||
width = _tex_gpu->w;
|
width = _tex_gpu->w;
|
||||||
height = _tex_gpu->h;
|
height = _tex_gpu->h;
|
||||||
|
color.r = 255;
|
||||||
|
color.g = 255;
|
||||||
|
color.b = 255;
|
||||||
}
|
}
|
||||||
SDL_Rect emptyRect = {0, 0, 0, 0};
|
|
||||||
GPU_Rect *r;
|
GPU_Rect *r;
|
||||||
virtual void update(float elapsed) {
|
virtual void update(float elapsed) {
|
||||||
_x = x+offset.x;
|
_x = x+offset.x;
|
||||||
_y = y+offset.y;
|
_y = y+offset.y;
|
||||||
_w = width*scale.x;
|
GPU_SetRGBA(_tex_gpu, color.r, color.g, color.b, alpha*255);
|
||||||
_h = height*scale.y;
|
|
||||||
|
|
||||||
_sc.x = _x-_camera->x;
|
|
||||||
_sc.y = _y-_camera->y;
|
|
||||||
_sc.w = _w;
|
|
||||||
_sc.h = _h;
|
|
||||||
|
|
||||||
// GPU_SetRGBA(_tex_gpu, 255, 255, 255, alpha*255);
|
|
||||||
|
|
||||||
r = &_src_rect;
|
r = &_src_rect;
|
||||||
|
|
||||||
if (r->w == 0)
|
if (r->w == 0)
|
||||||
r = NULL;
|
r = NULL;
|
||||||
GPU_BlitRectX(_tex_gpu, r, mContext.gpu_render, &_sc, angle, NULL, NULL, GPU_FLIP_NONE);
|
GPU_Rect dst = {static_cast<float>(_x-_camera->x), static_cast<float>(_y-_camera->y), width*scale.x, height*scale.y};
|
||||||
|
GPU_BlitRectX(_tex_gpu, r, mContext.gpu_render, &dst, angle, NULL, NULL, GPU_FLIP_NONE);
|
||||||
}
|
}
|
||||||
virtual void destroy() {
|
virtual void destroy() {
|
||||||
_x = 0;
|
_x = 0;
|
||||||
_y = 0;
|
_y = 0;
|
||||||
_w = 0;
|
_w = 0;
|
||||||
_h = 0;
|
_h = 0;
|
||||||
_sc = {0, 0, 0, 0};
|
|
||||||
GPU_FreeImage(_tex_gpu);
|
GPU_FreeImage(_tex_gpu);
|
||||||
}
|
}
|
||||||
virtual void updateCamera(SDL_Rect* camera) {
|
virtual void updateCamera(SDL_Rect* camera) {
|
||||||
|
@ -213,7 +236,6 @@ namespace sdfml {
|
||||||
protected:
|
protected:
|
||||||
int _x, _y, _w, _h;
|
int _x, _y, _w, _h;
|
||||||
|
|
||||||
GPU_Rect _sc;
|
|
||||||
GPU_Rect _src_rect = {0, 0, 0, 0};
|
GPU_Rect _src_rect = {0, 0, 0, 0};
|
||||||
SDL_Rect dummy = {0, 0, 0, 0};
|
SDL_Rect dummy = {0, 0, 0, 0};
|
||||||
sdCam _cam;
|
sdCam _cam;
|
||||||
|
@ -316,7 +338,10 @@ namespace sdfml {
|
||||||
vector<sdSprite> get_mspr() {
|
vector<sdSprite> get_mspr() {
|
||||||
return _mut_sprites;
|
return _mut_sprites;
|
||||||
}
|
}
|
||||||
private:
|
void freeSprites() {
|
||||||
|
_mut_sprites.clear();
|
||||||
|
_sprites.clear();
|
||||||
|
}
|
||||||
vector<sdSprite> _mut_sprites;
|
vector<sdSprite> _mut_sprites;
|
||||||
vector<sdSprite*> _sprites;
|
vector<sdSprite*> _sprites;
|
||||||
};
|
};
|
||||||
|
@ -423,14 +448,22 @@ namespace sdfml {
|
||||||
static const Uint8* kb_last;
|
static const Uint8* kb_last;
|
||||||
|
|
||||||
inline bool key_just_pressed(SDL_Scancode code) {
|
inline bool key_just_pressed(SDL_Scancode code) {
|
||||||
if (kb[code] && !kb_last[code])
|
while (SDL_PollEvent(&mContext.events))
|
||||||
|
{
|
||||||
|
if (mContext.events.type == SDL_KEYDOWN)
|
||||||
|
if (mContext.events.key.keysym.scancode == code)
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool key_just_released(SDL_Scancode code) {
|
inline bool key_just_released(SDL_Scancode code) {
|
||||||
if (!kb[code] && kb_last[code])
|
while (SDL_PollEvent(&mContext.events))
|
||||||
|
{
|
||||||
|
if (mContext.events.type == SDL_KEYUP)
|
||||||
|
if (mContext.events.key.keysym.scancode == code)
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -502,12 +535,12 @@ namespace sdfml {
|
||||||
|
|
||||||
float elapsedMS = (float)(end - start) / SDL_GetPerformanceFrequency() * 1000.0f;
|
float elapsedMS = (float)(end - start) / SDL_GetPerformanceFrequency() * 1000.0f;
|
||||||
|
|
||||||
kb_last = SDL_GetKeyboardState(NULL);
|
|
||||||
|
|
||||||
elapsed += 1;
|
elapsed += 1;
|
||||||
|
|
||||||
SDL_Delay(floor((1000.0f/FRAMERATE) - elapsedMS));
|
SDL_Delay(floor((1000.0f/FRAMERATE) - elapsedMS));
|
||||||
|
|
||||||
|
kb_last = SDL_GetKeyboardState(NULL);
|
||||||
|
|
||||||
GPU_Flip(mContext.gpu_render);
|
GPU_Flip(mContext.gpu_render);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -530,12 +563,18 @@ namespace sdfml {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void switchState(sdState* state) {
|
static sdSprite transitionSprite;
|
||||||
|
static sdSprite transitionSprite2;
|
||||||
|
static sdTimer fadeTimer;
|
||||||
|
static sdTimer switchTimer;
|
||||||
|
|
||||||
|
inline double clamp(double d, double min, double max) {
|
||||||
|
const double t = d < min ? min : d;
|
||||||
|
return t > max ? max : t;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline int lmao(sdState* state) {
|
||||||
if (curState != nullptr) {
|
if (curState != nullptr) {
|
||||||
_ticks = {};
|
|
||||||
_call = {};
|
|
||||||
_repeats = {};
|
|
||||||
_sec = {};
|
|
||||||
if (curState->get_spr().size() > 0) {
|
if (curState->get_spr().size() > 0) {
|
||||||
for (auto texture : curState->get_spr()) {
|
for (auto texture : curState->get_spr()) {
|
||||||
texture->destroy();
|
texture->destroy();
|
||||||
|
@ -546,9 +585,44 @@ namespace sdfml {
|
||||||
texture.destroy();
|
texture.destroy();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
curState->freeSprites();
|
||||||
}
|
}
|
||||||
curState = state;
|
curState = state;
|
||||||
|
_ticks = {};
|
||||||
|
_call = {};
|
||||||
|
_repeats = {};
|
||||||
|
_sec = {};
|
||||||
curState->create();
|
curState->create();
|
||||||
|
transitionSprite2.create(0, 0, "data/images/black.png");
|
||||||
|
curState->add(&transitionSprite2);
|
||||||
|
fadeTimer.start(0, []() {
|
||||||
|
transitionSprite2.x = clamp(transitionSprite2.x - 10, -mContext.size.x, 0);
|
||||||
|
return 0;
|
||||||
|
}, true);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void switchState(sdState* state) {
|
||||||
|
try {
|
||||||
|
if (curState != nullptr) {
|
||||||
|
transitionSprite.create(mContext.size.x, 0, "data/images/black.png");
|
||||||
|
curState->add(&transitionSprite);
|
||||||
|
fadeTimer.start(0, []() {
|
||||||
|
transitionSprite.x = clamp(transitionSprite.x - 7, 0, mContext.size.x);
|
||||||
|
return 0;
|
||||||
|
}, true);
|
||||||
|
switchTimer.start(1, [state](){
|
||||||
|
lmao(state);
|
||||||
|
return 0;
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
lmao(state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (...) {
|
||||||
|
lmao(state);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
Loading…
Reference in a new issue