- [NEW] Mogut tot el codi de lloc
This commit is contained in:
133
source/japi/game.cpp
Normal file
133
source/japi/game.cpp
Normal file
@@ -0,0 +1,133 @@
|
||||
#include "game.h"
|
||||
#include "draw.h"
|
||||
#include "input.h"
|
||||
#include <SDL3/SDL.h>
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
#ifdef MACOS_BUNDLE
|
||||
#include <libgen.h>
|
||||
#endif
|
||||
|
||||
namespace game
|
||||
{
|
||||
bool windowHasFocus = true;
|
||||
|
||||
static bool (*loop)() = nullptr;
|
||||
static unsigned int ticks_per_frame = 1000/60;
|
||||
static std::map<std::string, int> config;
|
||||
|
||||
void setUpdateTicks(const int ticks)
|
||||
{
|
||||
ticks_per_frame = ticks;
|
||||
}
|
||||
|
||||
void setState(bool (*loop)())
|
||||
{
|
||||
game::loop = loop;
|
||||
}
|
||||
|
||||
void setConfig(const char* key, const int value)
|
||||
{
|
||||
config[key] = value;
|
||||
}
|
||||
|
||||
const int getConfig(const char* key)
|
||||
{
|
||||
return config[key];
|
||||
}
|
||||
|
||||
const uint32_t getTicks()
|
||||
{
|
||||
return SDL_GetTicks();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
#ifdef MACOS_BUNDLE
|
||||
char res_file[255] = "";
|
||||
strcpy(res_file, dirname(argv[0]));
|
||||
strcat(res_file, "/../Resources/data.jf2");
|
||||
file_setresourcefilename(res_file);
|
||||
#endif
|
||||
|
||||
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO);
|
||||
|
||||
#ifdef DEBUG
|
||||
SDL_SetLogPriorities(SDL_LOG_PRIORITY_DEBUG);
|
||||
#endif
|
||||
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_SYSTEM, "JAPI v%s\n", JAPI_VERSION);
|
||||
|
||||
game::windowHasFocus = true;
|
||||
game::init();
|
||||
input::init();
|
||||
|
||||
static unsigned int current_ticks = SDL_GetTicks();
|
||||
|
||||
bool should_exit=false;
|
||||
SDL_Event e;
|
||||
while (!should_exit)
|
||||
{
|
||||
while(SDL_PollEvent(&e))
|
||||
{
|
||||
if (e.type==SDL_EVENT_QUIT)
|
||||
{
|
||||
should_exit = true;
|
||||
break;
|
||||
}
|
||||
if (e.type==SDL_EVENT_KEY_DOWN)
|
||||
{
|
||||
input::updateKey(e.key.scancode);
|
||||
}
|
||||
if (e.type==SDL_EVENT_KEY_UP)
|
||||
{
|
||||
switch (e.key.scancode) {
|
||||
case SDL_SCANCODE_F1:
|
||||
draw::setZoom(draw::getZoom()-1);
|
||||
break;
|
||||
case SDL_SCANCODE_F2:
|
||||
draw::setZoom(draw::getZoom()+1);
|
||||
break;
|
||||
case SDL_SCANCODE_F3:
|
||||
draw::setFullscreen(!draw::getFullscreen());
|
||||
break;
|
||||
case SDL_SCANCODE_F4:
|
||||
draw::toggleShader();
|
||||
break;
|
||||
default:
|
||||
input::updateKeypressed(e.key.scancode);
|
||||
}
|
||||
}
|
||||
if (e.type==SDL_EVENT_MOUSE_BUTTON_UP)
|
||||
{
|
||||
input::updateClk(e.button.button);
|
||||
}
|
||||
if (e.type==SDL_EVENT_MOUSE_WHEEL)
|
||||
{
|
||||
input::updateWheel(e.wheel.y);
|
||||
}
|
||||
if ( e.type == SDL_EVENT_WINDOW_FOCUS_GAINED )
|
||||
{
|
||||
game::windowHasFocus = true;
|
||||
}
|
||||
if ( e.type == SDL_EVENT_WINDOW_FOCUS_LOST )
|
||||
{
|
||||
game::windowHasFocus = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (SDL_GetTicks()-current_ticks >= game::ticks_per_frame)
|
||||
{
|
||||
if (game::loop) if (!game::loop()) should_exit = true;
|
||||
input::updateKey(SDL_SCANCODE_UNKNOWN);
|
||||
input::updateKeypressed(SDL_SCANCODE_UNKNOWN);
|
||||
input::updateClk(0);
|
||||
input::updateWheel(0);
|
||||
current_ticks = SDL_GetTicks();
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user