SDfmL/src/Main.cpp

166 lines
3.8 KiB
C++
Raw Normal View History

2022-02-11 01:06:31 +00:00
/**
* @file Main.cpp
* @author haya3218
* @brief This is an example on how to use SDfmL. Code may suck. beware
* @version 0.1
* @date 2022-02-11
*
* @copyright Copyright (c) 2022
*
*/
#define SDL_STBIMAGE_IMPLEMENTATION // to use sdl_stbimage
#undef _HAS_STD_BYTE // avoid std::byte hijacking native byte type
2022-02-07 05:15:50 +00:00
#include <iostream>
#include "SDL2/SDL.h"
2022-02-11 02:10:15 +00:00
#undef main
2022-02-07 05:15:50 +00:00
#include <cmath>
#include <Windows.h>
2022-02-11 02:10:15 +00:00
#include <stdlib.h>
#include "argh.h"
#include "guicon.h"
2022-02-11 07:38:31 +00:00
#include <random>
2022-02-07 05:15:50 +00:00
#include "libflixel.hpp"
class ExampleState : public sdfml::sdState {
public:
sdfml::sdAnimatedSprite example;
2022-02-15 01:11:07 +00:00
sdfml::sdSprite bg1;
SDL_Rect camera = {0, 0, 640, 480};
// vector<sdfml::sdSprite> bgs;
virtual void create() {
example.create(50, 50, "data/images/smile.png");
example.AddAnimation("idle", {{0, 0, 50, 50}, {50, 0, 50, 50}});
example.PlayAnimation("idle");
example.framerate = 1;
2022-02-16 00:47:39 +00:00
sdfml::sound.music.playMusic("data/music/canyon.ogg");
2022-02-15 01:11:07 +00:00
example.updateCamera(&camera);
2022-02-16 00:47:39 +00:00
bg1.create(0, 0, "data/images/bg.png");
add(&bg1);
bg1.updateCamera(&camera);
add(&example);
2022-02-16 06:04:01 +00:00
//bg1.screenCenter();
}
virtual void update(float elapsed) {
if (sdfml::key_pressed(SDL_SCANCODE_LEFT))
example.x -= 1;
if (sdfml::key_pressed(SDL_SCANCODE_RIGHT))
example.x += 1;
if (sdfml::key_pressed(SDL_SCANCODE_UP))
example.y -= 1;
if (sdfml::key_pressed(SDL_SCANCODE_DOWN))
example.y += 1;
2022-02-16 06:04:01 +00:00
if (sdfml::key_just_pressed(SDL_SCANCODE_R))
sdfml::switchState(this);
2022-02-16 00:47:39 +00:00
bg1.scale.x = sin(sdfml::elapsed/100);
bg1.scale.y = cos(sdfml::elapsed/100);
2022-02-16 06:04:01 +00:00
2022-02-16 00:47:39 +00:00
bg1.screenCenter();
2022-02-15 01:11:07 +00:00
sdfml::focusCamera(&camera, example);
2022-02-11 07:38:31 +00:00
}
};
ExampleState m;
2022-02-11 02:10:15 +00:00
int main(int argc, char* argv[])
{
if (sdfml::init() > 0) {
2022-02-11 02:10:15 +00:00
return EXIT_FAILURE;
}
2022-02-07 05:15:50 +00:00
argh::parser pa(argc, argv);
// Verbose redirects io output to console, if available
if (pa[{"-v", "--verbose"}]) {
RedirectIOToConsole();
2022-02-15 01:11:07 +00:00
sdfml::llog("Verbose mode", " enabled.", NORMAL, __FILENAME__, __LINE__);
}
2022-02-11 02:10:15 +00:00
sdfml::switchState(&m);
2022-02-07 05:15:50 +00:00
if (sdfml::update() > 0) {
return EXIT_FAILURE;
2022-02-07 05:15:50 +00:00
}
2022-02-11 02:10:15 +00:00
return EXIT_SUCCESS;
}
// This is the main entry point of a program. Currently all this does is parse arguments and call main().
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, PSTR cmdline, int cmdshow)
{
int argc;
char** argv;
char* arg;
int index;
int result;
// count the arguments
argc = 1;
arg = cmdline;
while (arg[0] != 0) {
while (arg[0] != 0 && arg[0] == ' ') {
arg++;
}
if (arg[0] != 0) {
argc++;
while (arg[0] != 0 && arg[0] != ' ') {
arg++;
}
}
}
// tokenize the arguments
argv = (char**)malloc(argc * sizeof(char*));
arg = cmdline;
index = 1;
while (arg[0] != 0) {
while (arg[0] != 0 && arg[0] == ' ') {
arg++;
}
if (arg[0] != 0) {
argv[index] = arg;
index++;
while (arg[0] != 0 && arg[0] != ' ') {
arg++;
}
if (arg[0] != 0) {
arg[0] = 0;
arg++;
}
}
}
// put the program name into argv[0]
char filename[_MAX_PATH];
GetModuleFileName(NULL, filename, _MAX_PATH);
argv[0] = filename;
// call the user specified main function
result = main(argc, argv);
free(argv);
return result;
2022-02-07 05:15:50 +00:00
}