mirror of
https://github.com/haya3218/SDfmL.git
synced 2024-08-14 23:57:09 +00:00
New stuff
$- Refined camera a lil bit\n- Alpha support :)\n- Splash screem :O
This commit is contained in:
parent
92da631aeb
commit
2120f83e1c
7 changed files with 96 additions and 26 deletions
|
@ -36,6 +36,8 @@ You know how to place them. It's fucking common sense.
|
|||
- After successfully making the build files, run `cmake --build --config Release` to build it without the use of VStudio.
|
||||
- If everything worked well, congratulations! You have just compiled a shitty program.
|
||||
|
||||
`src/Main.cpp` should contain example code with some explanations.
|
||||
|
||||
# Licensing
|
||||
|
||||
The sole libraries themselves (SDL and BASS) are under different licenses ofc, so you'll have to deal with that
|
||||
|
|
BIN
bin/data/black.png
Normal file
BIN
bin/data/black.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 339 B |
BIN
bin/data/flixel.ogg
Normal file
BIN
bin/data/flixel.ogg
Normal file
Binary file not shown.
BIN
bin/data/powered.png
Normal file
BIN
bin/data/powered.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1,001 B |
89
src/Main.cpp
89
src/Main.cpp
|
@ -17,24 +17,29 @@ class MainState : public State {
|
|||
Object title2;
|
||||
Object title3;
|
||||
Object title4;
|
||||
// cameras are rects
|
||||
SDL_Rect camera = {0, 0, WINDOW_WIDTH, WINDOW_HEIGHT};
|
||||
// to override a function, just state back the name of it with a virtual keyword
|
||||
// before it
|
||||
virtual void Create() {
|
||||
playMusic("data/canyon.ogg");
|
||||
// i know that this isnt the best idea to do this
|
||||
// yeah, im just lazy
|
||||
title.create(0, 0, "data/bg.png");
|
||||
AddObject(&title);
|
||||
|
||||
title2.create(640, 0, "data/bg.png");
|
||||
AddObject(&title2);
|
||||
|
||||
title3.create(640, 480, "data/bg.png");
|
||||
AddObject(&title3);
|
||||
|
||||
title4.create(0, 480, "data/bg.png");
|
||||
AddObject(&title4);
|
||||
// example of animated object
|
||||
objs.create(0, 0, "data/smile.png");
|
||||
AddObject(&objs);
|
||||
objs.AddAnimation("idle", {{0, 0, 50, 50}, {50, 0, 50, 50}});
|
||||
objs.PlayAnimation("idle");
|
||||
objs.framerate = 1;
|
||||
// set cameras :)
|
||||
objs.setCamera(&camera);
|
||||
title.setCamera(&camera);
|
||||
title2.setCamera(&camera);
|
||||
|
@ -42,63 +47,105 @@ class MainState : public State {
|
|||
title4.setCamera(&camera);
|
||||
title.scale.y = 0;
|
||||
|
||||
// center object
|
||||
objs.centerSelf();
|
||||
|
||||
title.center.x = WINDOW_WIDTH/2;
|
||||
title.center.y = WINDOW_HEIGHT/2;
|
||||
|
||||
// offsets :)
|
||||
title2.offset.x = 640;
|
||||
title3.offset = {640, 480};
|
||||
title4.offset.y = 480;
|
||||
|
||||
// example of a text object
|
||||
text.create(50, 480 - 100, "Welcome to the funny application\nMake yourself at home :)\n- haya", "data/monogram.ttf", {255, 255, 255, 255}, TTF_STYLE_NORMAL, 40);
|
||||
AddObject(&text);
|
||||
}
|
||||
int yvel = 0;
|
||||
int xvel = 0;
|
||||
float elaped = 0;
|
||||
// update
|
||||
virtual void Update(float dt) {
|
||||
// to get current keyboard input, use SDL_GetKeyboardState
|
||||
const Uint8* kb = SDL_GetKeyboardState(NULL);
|
||||
|
||||
// :)
|
||||
yvel = -kb[SDL_SCANCODE_UP] + kb[SDL_SCANCODE_DOWN];
|
||||
xvel = -kb[SDL_SCANCODE_LEFT] + kb[SDL_SCANCODE_RIGHT];
|
||||
objs.x = objs.x + xvel*2;
|
||||
objs.y = objs.y + yvel*2;
|
||||
|
||||
title.scale.y = sin(elaped);
|
||||
title.scale.x = cos(elaped);
|
||||
title2.scale.y = sin(elaped);
|
||||
title2.scale.x = cos(elaped);
|
||||
title3.scale.y = sin(elaped);
|
||||
title3.scale.x = cos(elaped);
|
||||
title4.scale.y = sin(elaped);
|
||||
title4.scale.x = cos(elaped);
|
||||
// cool scaling
|
||||
title.scale.y = sin(elaped/100);
|
||||
title.scale.x = cos(elaped/100);
|
||||
title2.scale.y = cos(elaped/100);
|
||||
title2.scale.x = sin(elaped/100);
|
||||
title3.scale.y = sin(elaped/100);
|
||||
title3.scale.x = sin(elaped/100);
|
||||
title4.scale.y = cos(elaped/100);
|
||||
title4.scale.x = cos(elaped/100);
|
||||
|
||||
// make camera point to an object
|
||||
pointTo(&camera, objs);
|
||||
|
||||
// center shit
|
||||
title.centerSelf();
|
||||
title2.centerSelf();
|
||||
title3.centerSelf();
|
||||
title4.centerSelf();
|
||||
text.centerSelf();
|
||||
|
||||
text.scale.y = cos(elaped*10)*2;
|
||||
// :)
|
||||
text.scale.y = cos(elaped/10)*2;
|
||||
text.offset.y = -240/2;
|
||||
|
||||
elaped += 0.01;
|
||||
// for the sin and cos shit
|
||||
elaped += 1;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
MainState m2;
|
||||
|
||||
class SplashScreen : public State {
|
||||
Object bg;
|
||||
Object black;
|
||||
int tick[2];
|
||||
virtual void Create() {
|
||||
bg.create(0, 0, "data/powered.png");
|
||||
AddObject(&bg);
|
||||
|
||||
black.create(0, 0, "data/black.png");
|
||||
AddObject(&black);
|
||||
|
||||
black.alpha = 0;
|
||||
|
||||
playSound("data/flixel.ogg");
|
||||
|
||||
// basic timer
|
||||
tick[0] = Sec2Tick(1.0);
|
||||
tick[1] = Sec2Tick(3.0);
|
||||
|
||||
//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;
|
||||
|
||||
int main() {
|
||||
Init("SDLflixel :)))))");
|
||||
|
||||
MainState m;
|
||||
|
||||
SwitchState(&m);
|
||||
|
||||
playMusic("data/canyon.ogg");
|
||||
|
||||
if (!Update()) {
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -15,7 +15,7 @@ using namespace Render;
|
|||
SDL_Window* Render::window;
|
||||
SDL_Renderer* Render::renderer;
|
||||
SDL_Event Render::event;
|
||||
State* Render::current_state;
|
||||
State* Render::current_state = nullptr;
|
||||
array<anshub::AudioOut, MAX_SE> Render::audioArray;
|
||||
anshub::AudioOut Render::music;
|
||||
string Render::currentMusic = "";
|
||||
|
@ -40,6 +40,7 @@ Render::State::~State() {
|
|||
|
||||
void Render::Object::create(int x, int y, string path){
|
||||
this->_tex = IMG_LoadTexture(renderer, path.c_str());
|
||||
SDL_SetTextureBlendMode(_tex, SDL_BLENDMODE_BLEND);
|
||||
if (_tex == nullptr) {
|
||||
cout << "texture failed to lod" << endl;
|
||||
}
|
||||
|
@ -82,6 +83,11 @@ void Render::Object::Draw(float dt) {
|
|||
_sc_w = _w;
|
||||
_h = h*scale.y;
|
||||
_sc_h = _h;
|
||||
|
||||
if (alpha > 100)
|
||||
alpha = 100;
|
||||
|
||||
SDL_SetTextureAlphaMod(this->_tex, (this->alpha/100)*255);
|
||||
}
|
||||
|
||||
void Render::Object::setCamera(SDL_Rect* cam_p) {
|
||||
|
@ -259,6 +265,11 @@ bool Render::Update() {
|
|||
}
|
||||
|
||||
void Render::SwitchState(State* state) {
|
||||
if (current_state != nullptr) {
|
||||
for (int i = 0; i < current_state->get_obj().size(); i++) {
|
||||
SDL_DestroyTexture(current_state->get_obj()[i]->_tex);
|
||||
}
|
||||
}
|
||||
current_state = state;
|
||||
current_state->Create();
|
||||
}
|
||||
|
@ -268,7 +279,7 @@ bool Render::playSound(string path, int id) {
|
|||
for (int i = 0; i < MAX_SE; i++) {
|
||||
if (audioArray[i].NowPlaying(false).size() == 0) {
|
||||
audioArray[i].Play(path);
|
||||
cout << "Played dat boi. audio id no: " << i << endl;
|
||||
cout << "Played " << path << ". audio id no: " << i << endl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -282,10 +293,14 @@ bool Render::playSound(string path, int id) {
|
|||
bool Render::playMusic(string path) {
|
||||
if (path == "") {
|
||||
music.Stop(currentMusic);
|
||||
cout << "Stopped music from " << path << "." << endl;
|
||||
return true;
|
||||
}
|
||||
if (currentMusic != "") {
|
||||
music.Stop(currentMusic);
|
||||
cout << "Stopped music from " << path << "." << endl;
|
||||
}
|
||||
cout << "Played " << path << " as music." << endl;
|
||||
music.Play(path, true);
|
||||
currentMusic = path;
|
||||
|
||||
|
@ -293,7 +308,7 @@ bool Render::playMusic(string path) {
|
|||
}
|
||||
|
||||
void Render::pointTo(SDL_Rect* camera, Object object) {
|
||||
camera->x = ( object.x + (object.w / 2)/2 ) - WINDOW_WIDTH / 2;
|
||||
camera->x = ( object.x + (object.w / 2) ) - WINDOW_WIDTH / 2;
|
||||
camera->y = ( object.y + (object.h / 2) ) - WINDOW_HEIGHT / 2;
|
||||
|
||||
if( camera->x < 0 )
|
||||
|
|
|
@ -67,6 +67,8 @@ namespace Render {
|
|||
|
||||
float angle = 0.00001;
|
||||
|
||||
float alpha = 100;
|
||||
|
||||
SDL_Point center = {0, 0};
|
||||
|
||||
map<string, bool> get_properties() const;
|
||||
|
@ -216,5 +218,9 @@ namespace Render {
|
|||
* Make the camera center itself on an object.
|
||||
*/
|
||||
void pointTo(SDL_Rect* camera, Object object);
|
||||
|
||||
inline int Sec2Tick(float time) {
|
||||
return FRAMERATE*time;
|
||||
}
|
||||
}
|
||||
#endif
|
Loading…
Reference in a new issue