New stuff

$- Refined camera a lil bit\n- Alpha support :)\n- Splash screem :O
This commit is contained in:
/nick haya 2022-02-08 13:54:19 +08:00
parent 92da631aeb
commit 2120f83e1c
7 changed files with 96 additions and 26 deletions

View file

@ -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. - 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. - If everything worked well, congratulations! You have just compiled a shitty program.
`src/Main.cpp` should contain example code with some explanations.
# Licensing # Licensing
The sole libraries themselves (SDL and BASS) are under different licenses ofc, so you'll have to deal with that 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

Binary file not shown.

After

Width:  |  Height:  |  Size: 339 B

BIN
bin/data/flixel.ogg Normal file

Binary file not shown.

BIN
bin/data/powered.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1,001 B

View file

@ -17,24 +17,29 @@ class MainState : public State {
Object title2; Object title2;
Object title3; Object title3;
Object title4; Object title4;
// cameras are rects
SDL_Rect camera = {0, 0, WINDOW_WIDTH, WINDOW_HEIGHT}; 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() { 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"); title.create(0, 0, "data/bg.png");
AddObject(&title); AddObject(&title);
title2.create(640, 0, "data/bg.png"); title2.create(640, 0, "data/bg.png");
AddObject(&title2); AddObject(&title2);
title3.create(640, 480, "data/bg.png"); title3.create(640, 480, "data/bg.png");
AddObject(&title3); AddObject(&title3);
title4.create(0, 480, "data/bg.png"); title4.create(0, 480, "data/bg.png");
AddObject(&title4); AddObject(&title4);
// example of animated object
objs.create(0, 0, "data/smile.png"); objs.create(0, 0, "data/smile.png");
AddObject(&objs); AddObject(&objs);
objs.AddAnimation("idle", {{0, 0, 50, 50}, {50, 0, 50, 50}}); objs.AddAnimation("idle", {{0, 0, 50, 50}, {50, 0, 50, 50}});
objs.PlayAnimation("idle"); objs.PlayAnimation("idle");
objs.framerate = 1; objs.framerate = 1;
// set cameras :)
objs.setCamera(&camera); objs.setCamera(&camera);
title.setCamera(&camera); title.setCamera(&camera);
title2.setCamera(&camera); title2.setCamera(&camera);
@ -42,63 +47,105 @@ class MainState : public State {
title4.setCamera(&camera); title4.setCamera(&camera);
title.scale.y = 0; title.scale.y = 0;
// center object
objs.centerSelf(); objs.centerSelf();
title.center.x = WINDOW_WIDTH/2; // offsets :)
title.center.y = WINDOW_HEIGHT/2;
title2.offset.x = 640; title2.offset.x = 640;
title3.offset = {640, 480}; title3.offset = {640, 480};
title4.offset.y = 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); 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); AddObject(&text);
} }
int yvel = 0; int yvel = 0;
int xvel = 0; int xvel = 0;
float elaped = 0; float elaped = 0;
// update
virtual void Update(float dt) { virtual void Update(float dt) {
// to get current keyboard input, use SDL_GetKeyboardState
const Uint8* kb = SDL_GetKeyboardState(NULL); const Uint8* kb = SDL_GetKeyboardState(NULL);
// :)
yvel = -kb[SDL_SCANCODE_UP] + kb[SDL_SCANCODE_DOWN]; yvel = -kb[SDL_SCANCODE_UP] + kb[SDL_SCANCODE_DOWN];
xvel = -kb[SDL_SCANCODE_LEFT] + kb[SDL_SCANCODE_RIGHT]; xvel = -kb[SDL_SCANCODE_LEFT] + kb[SDL_SCANCODE_RIGHT];
objs.x = objs.x + xvel*2; objs.x = objs.x + xvel*2;
objs.y = objs.y + yvel*2; objs.y = objs.y + yvel*2;
title.scale.y = sin(elaped); // cool scaling
title.scale.x = cos(elaped); title.scale.y = sin(elaped/100);
title2.scale.y = sin(elaped); title.scale.x = cos(elaped/100);
title2.scale.x = cos(elaped); title2.scale.y = cos(elaped/100);
title3.scale.y = sin(elaped); title2.scale.x = sin(elaped/100);
title3.scale.x = cos(elaped); title3.scale.y = sin(elaped/100);
title4.scale.y = sin(elaped); title3.scale.x = sin(elaped/100);
title4.scale.x = cos(elaped); title4.scale.y = cos(elaped/100);
title4.scale.x = cos(elaped/100);
// make camera point to an object
pointTo(&camera, objs); pointTo(&camera, objs);
// center shit
title.centerSelf(); title.centerSelf();
title2.centerSelf(); title2.centerSelf();
title3.centerSelf(); title3.centerSelf();
title4.centerSelf(); title4.centerSelf();
text.centerSelf(); text.centerSelf();
text.scale.y = cos(elaped*10)*2; // :)
text.scale.y = cos(elaped/10)*2;
text.offset.y = -240/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() { int main() {
Init("SDLflixel :)))))"); Init("SDLflixel :)))))");
MainState m;
SwitchState(&m); SwitchState(&m);
playMusic("data/canyon.ogg");
if (!Update()) { if (!Update()) {
return 0; return 0;
} }

View file

@ -15,7 +15,7 @@ using namespace Render;
SDL_Window* Render::window; SDL_Window* Render::window;
SDL_Renderer* Render::renderer; SDL_Renderer* Render::renderer;
SDL_Event Render::event; SDL_Event Render::event;
State* Render::current_state; State* Render::current_state = nullptr;
array<anshub::AudioOut, MAX_SE> Render::audioArray; array<anshub::AudioOut, MAX_SE> Render::audioArray;
anshub::AudioOut Render::music; anshub::AudioOut Render::music;
string Render::currentMusic = ""; string Render::currentMusic = "";
@ -40,6 +40,7 @@ Render::State::~State() {
void Render::Object::create(int x, int y, string path){ void Render::Object::create(int x, int y, string path){
this->_tex = IMG_LoadTexture(renderer, path.c_str()); this->_tex = IMG_LoadTexture(renderer, path.c_str());
SDL_SetTextureBlendMode(_tex, SDL_BLENDMODE_BLEND);
if (_tex == nullptr) { if (_tex == nullptr) {
cout << "texture failed to lod" << endl; cout << "texture failed to lod" << endl;
} }
@ -82,6 +83,11 @@ void Render::Object::Draw(float dt) {
_sc_w = _w; _sc_w = _w;
_h = h*scale.y; _h = h*scale.y;
_sc_h = _h; _sc_h = _h;
if (alpha > 100)
alpha = 100;
SDL_SetTextureAlphaMod(this->_tex, (this->alpha/100)*255);
} }
void Render::Object::setCamera(SDL_Rect* cam_p) { void Render::Object::setCamera(SDL_Rect* cam_p) {
@ -259,6 +265,11 @@ bool Render::Update() {
} }
void Render::SwitchState(State* state) { 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 = state;
current_state->Create(); current_state->Create();
} }
@ -268,7 +279,7 @@ bool Render::playSound(string path, int id) {
for (int i = 0; i < MAX_SE; i++) { for (int i = 0; i < MAX_SE; i++) {
if (audioArray[i].NowPlaying(false).size() == 0) { if (audioArray[i].NowPlaying(false).size() == 0) {
audioArray[i].Play(path); audioArray[i].Play(path);
cout << "Played dat boi. audio id no: " << i << endl; cout << "Played " << path << ". audio id no: " << i << endl;
break; break;
} }
} }
@ -282,10 +293,14 @@ bool Render::playSound(string path, int id) {
bool Render::playMusic(string path) { bool Render::playMusic(string path) {
if (path == "") { if (path == "") {
music.Stop(currentMusic); music.Stop(currentMusic);
cout << "Stopped music from " << path << "." << endl;
return true;
} }
if (currentMusic != "") { if (currentMusic != "") {
music.Stop(currentMusic); music.Stop(currentMusic);
cout << "Stopped music from " << path << "." << endl;
} }
cout << "Played " << path << " as music." << endl;
music.Play(path, true); music.Play(path, true);
currentMusic = path; currentMusic = path;
@ -293,7 +308,7 @@ bool Render::playMusic(string path) {
} }
void Render::pointTo(SDL_Rect* camera, Object object) { 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; camera->y = ( object.y + (object.h / 2) ) - WINDOW_HEIGHT / 2;
if( camera->x < 0 ) if( camera->x < 0 )

View file

@ -67,6 +67,8 @@ namespace Render {
float angle = 0.00001; float angle = 0.00001;
float alpha = 100;
SDL_Point center = {0, 0}; SDL_Point center = {0, 0};
map<string, bool> get_properties() const; map<string, bool> get_properties() const;
@ -216,5 +218,9 @@ namespace Render {
* Make the camera center itself on an object. * Make the camera center itself on an object.
*/ */
void pointTo(SDL_Rect* camera, Object object); void pointTo(SDL_Rect* camera, Object object);
inline int Sec2Tick(float time) {
return FRAMERATE*time;
}
} }
#endif #endif