1
0
Fork 0
blockofighter/src/main.cpp

219 lines
4.7 KiB
C++

#include "main.h"
#include <SDL.h>
#include <stdlib.h>
#include <stdio.h>
#ifdef WIN32
#include <windows.h>
#endif
#include "audio.h"
#include "fonct.h"
#include "run.h"
#include "texture.h"
#include "fight.h"
#include "font.h"
#include "3dutils.h"
#include "glapi.h"
SDL_Window *window;
int screenwidth = 1024;
int screenheight = 768;
// static float speedfactor = 1.0;
#define FRAMESNEEDED_DEFAULT 6
static int framesneeded = FRAMESNEEDED_DEFAULT;
#define FPS 30
#define FPS_RELEASETIME (1000 / FPS)
void exitProgram(int code) {
SDL_Quit();
// uninitAudio();
exit(code);
}
void changeResolution(int width, int height, bool fullscreen) {
int mode = SDL_WINDOW_OPENGL;
if (fullscreen)
mode |= SDL_WINDOW_FULLSCREEN;
window = SDL_CreateWindow("BlockoFighter 2", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, mode);
if (!window) {
fprintf(stderr, "Couldn't set %i*%i opengl video mode: %s\n",
screenwidth, screenheight, SDL_GetError());
exitProgram(-1);
}
if (!SDL_GL_CreateContext(window)) {
fprintf(stderr, "Couldn't create OpenGL context: %s\n", SDL_GetError());
exitProgram(-1);
}
setupOpengl(width, height);
screenwidth = width;
screenheight = height;
if (fullscreen)
SDL_ShowCursor(SDL_DISABLE);
else
SDL_ShowCursor(SDL_ENABLE);
initScenes();
}
std::set<SDL_Keycode> keys;
void handleKeydown(SDL_Keysym *keysym) {
if (!keys.count(keysym->sym)) {
keys.insert(keysym->sym);
}
}
void handleKeyup(SDL_Keysym *keysym) {
auto it = keys.find(keysym->sym);
if (it != keys.end()) {
keys.erase(it);
}
}
void processEvents(void) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_KEYDOWN:
handleKeydown(&event.key.keysym);
break;
case SDL_KEYUP:
handleKeyup(&event.key.keysym);
break;
case SDL_WINDOWEVENT_RESIZED:
screenwidth = event.window.data1;
screenheight = event.window.data2;
setupOpengl(screenwidth, screenheight);
break;
case SDL_QUIT:
exitProgram(0);
break;
}
}
}
int getTime(void) {
//#ifdef WIN32
// return timeGetTime();
//#else
return SDL_GetTicks();
//#endif
}
void setSpeedFactor(float factor) {
framesneeded = (int)(factor * FRAMESNEEDED_DEFAULT);
}
int main(int argc, char *argv[]) {
// printf("Initializing SDL.\n");
if ((SDL_Init(SDL_INIT_VIDEO) == -1)) {
printf("Could not initialize SDL: %s.\n", SDL_GetError());
exitProgram(-1);
}
/*const SDL_VideoInfo *info = SDL_GetVideoInfo();
if (!info) {
printf("Could not get video info with SDL: %s.\n", SDL_GetError());
exitProgram(-1);
}*/
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
atexit(SDL_Quit);
initAudio();
changeResolution(1024, 768, false);
// printf("SDL initialized.\n");
/*double calculatefps = 200.0;
const int maxskipframes = 20;
int framecounter, absframecounter = 0;
int currenttime;
int framesdrawn=0;
int skipframes;
int starttime = getTime();
while (1){
do{
currenttime = getTime()-starttime;
} while (currenttime == 0);
framecounter = (int)((calculatefps/1000.0)*currenttime*speedfactor);
starttime = getTime();
skipframes = framecounter;
//ajustement de la vitesse pour ne pas accumuler le retard de frames
if (skipframes > maxskipframes)
{
if (calculatefps > 200.0)
{
calculatefps -= 20;
//printf("%f\n", calculatefps);
}
}
else
{
if (calculatefps < 200.0)
{
calculatefps += 20;
//printf("%f\n", calculatefps);
}
}
for (; skipframes > 0; skipframes--){
calculateFrame(++absframecounter);
}
//calculateFrame(absframecounter++);
processEvents();
drawFrame(absframecounter);
framesdrawn++;
}*/
unsigned int absframecounter = 0;
int i;
int currenttime;
int framesdrawn = 0;
while (1) {
currenttime = getTime();
// on calcule les frames physiques
for (i = 0; i < framesneeded; i++) {
calculateFrame(absframecounter++);
}
// on applique les évènements
processEvents();
// on trace l'image associée à la dernière frame physique
drawFrame(absframecounter);
framesdrawn++;
// on attend que le temps autorisé pour la frame graphique soit dépassé
while (getTime() - currenttime < FPS_RELEASETIME) {
}
}
return 0;
}