1
0
Fork 0

Updated to SDL2

This commit is contained in:
Michaël Lemaire 2021-06-09 01:10:09 +02:00
parent bf1f9192e5
commit 03b74c0dd0
14 changed files with 1088 additions and 112 deletions

2
.gitignore vendored
View file

@ -1,5 +1,7 @@
*.o *.o
*.user *.user
.vscode
Makefile
/CMakeCache.txt /CMakeCache.txt
/CMakeFiles/ /CMakeFiles/
/blockofighter /blockofighter

View file

@ -1,27 +1,26 @@
cmake_minimum_required(VERSION 3.10)
project(blockofighter) project(blockofighter)
cmake_minimum_required(VERSION 2.8)
aux_source_directory(src SRC_LIST) aux_source_directory(src SRC_LIST)
add_executable(${PROJECT_NAME} ${SRC_LIST}) add_executable(${PROJECT_NAME} ${SRC_LIST})
find_package(SDL REQUIRED) include(FindPkgConfig)
find_package(SDL_image REQUIRED) pkg_search_module(SDL2 REQUIRED sdl2)
pkg_search_module(SDL2IMAGE REQUIRED SDL2_image>=2.0.0)
pkg_search_module(SDL2MIXER REQUIRED SDL2_mixer>=2.0.0)
if(NOT SDL_FOUND) find_package(OpenGL)
message(FATAL_ERROR "SDL not found!")
endif(NOT SDL_FOUND)
if(NOT SDLIMAGE_FOUND)
message(FATAL_ERROR "SDL not found!")
endif(NOT SDLIMAGE_FOUND)
include_directories( include_directories(
${SDL_INCLUDE_DIR} ${SDL2_INCLUDE_DIRS}
${SDLIMAGE_INCLUDE_DIR} ${SDL2IMAGE_INCLUDE_DIRS}
${SDL2MIXER_INCLUDE_DIRS}
${OPENGL_INCLUDE_DIRS}
) )
target_link_libraries(blockofighter target_link_libraries(blockofighter
${SDL_LIBRARY} ${SDL2_LIBRARIES}
${SDLIMAGE_LIBRARY} ${SDL2IMAGE_LIBRARIES}
SDLmain ${SDL2MIXER_LIBRARIES}
GL ${OPENGL_LIBRARIES}
GLU
) )

1027
Makefile

File diff suppressed because it is too large Load diff

View file

@ -87,7 +87,7 @@ void calculateEnd(int framecount) {
endlight.setPosition(-sin(framecount * 0.007) * 10, 15, endlight.setPosition(-sin(framecount * 0.007) * 10, 15,
cos(framecount * 0.007) * 2 + 22); cos(framecount * 0.007) * 2 + 22);
if (keys[SDLK_ESCAPE]) { if (keys.count(SDLK_ESCAPE)) {
stopEnding(); stopEnding();
} }
} }

View file

@ -362,19 +362,19 @@ void startFight(void) {
points2 = 0; points2 = 0;
} }
SDLKey player1left = SDLK_LEFT; SDL_Keycode player1left = SDLK_LEFT;
SDLKey player1right = SDLK_RIGHT; SDL_Keycode player1right = SDLK_RIGHT;
SDLKey player1forward = SDLK_UP; SDL_Keycode player1forward = SDLK_UP;
SDLKey player1backward = SDLK_DOWN; SDL_Keycode player1backward = SDLK_DOWN;
SDLKey player1jump = SDLK_RSHIFT; SDL_Keycode player1jump = SDLK_RSHIFT;
SDLKey player1hit = SDLK_RCTRL; SDL_Keycode player1hit = SDLK_RCTRL;
SDLKey player2left = SDLK_f; SDL_Keycode player2left = SDLK_f;
SDLKey player2right = SDLK_h; SDL_Keycode player2right = SDLK_h;
SDLKey player2forward = SDLK_t; SDL_Keycode player2forward = SDLK_t;
SDLKey player2backward = SDLK_g; SDL_Keycode player2backward = SDLK_g;
SDLKey player2jump = SDLK_s; SDL_Keycode player2jump = SDLK_s;
SDLKey player2hit = SDLK_x; SDL_Keycode player2hit = SDLK_x;
void stopGame(void) { void stopGame(void) {
light1.setEnabled(false); light1.setEnabled(false);
@ -497,17 +497,17 @@ void calculateFight(int framecount) {
if (startcounter >= FIGHT) { if (startcounter >= FIGHT) {
if (man1->isAlive()) { if (man1->isAlive()) {
if (keys[player1left]) if (keys.count(player1left))
man1->turn(5); man1->turn(5);
if (keys[player1right]) if (keys.count(player1right))
man1->turn(-5); man1->turn(-5);
if (keys[player1forward]) if (keys.count(player1forward))
man1->walk(0.03); man1->walk(0.03);
if (keys[player1backward]) if (keys.count(player1backward))
man1->walk(-0.03); man1->walk(-0.03);
if (keys[player1jump]) if (keys.count(player1jump))
man1->jump(); man1->jump();
if (keys[player1hit]) { if (keys.count(player1hit)) {
man1->hit(); man1->hit();
man1->hitcounter = 1; man1->hitcounter = 1;
} else { } else {
@ -516,17 +516,17 @@ void calculateFight(int framecount) {
} }
if (man2->isAlive()) { if (man2->isAlive()) {
if (keys[player2left]) if (keys.count(player2left))
man2->turn(5); man2->turn(5);
if (keys[player2right]) if (keys.count(player2right))
man2->turn(-5); man2->turn(-5);
if (keys[player2forward]) if (keys.count(player2forward))
man2->walk(0.03); man2->walk(0.03);
if (keys[player2backward]) if (keys.count(player2backward))
man2->walk(-0.03); man2->walk(-0.03);
if (keys[player2jump]) if (keys.count(player2jump))
man2->jump(); man2->jump();
if (keys[player2hit]) { if (keys.count(player2hit)) {
man2->hit(); man2->hit();
man2->hitcounter = 1; man2->hitcounter = 1;
} else { } else {
@ -535,7 +535,7 @@ void calculateFight(int framecount) {
} }
} }
if (keys[SDLK_ESCAPE]) { if (keys.count(SDLK_ESCAPE)) {
setSpeedFactor(1.0); setSpeedFactor(1.0);
stopGame(); stopGame();
} }

View file

@ -25,7 +25,7 @@ void startFight(void);
void calculateFight(int framecount); void calculateFight(int framecount);
void drawFight(int framecount); void drawFight(int framecount);
void handleKeydownFight(SDLKey key); void handleKeydownFight(SDL_Keycode key);
void addGraphicsVector(float *p1, float *p2, float size); void addGraphicsVector(float *p1, float *p2, float size);
#endif #endif

View file

@ -2,6 +2,7 @@
#define __GLAPI_H_INCLUDED__ #define __GLAPI_H_INCLUDED__
#include <SDL_opengl.h> #include <SDL_opengl.h>
#include <GL/glu.h>
void setupOpengl(int width, int height); void setupOpengl(int width, int height);

View file

@ -1,6 +1,8 @@
#include "graphics.h" #include "graphics.h"
#include <stdlib.h> #include <stdlib.h>
#include "texture.h"
GraphicsDruid *GraphicsDruid::instance = 0; GraphicsDruid *GraphicsDruid::instance = 0;
GraphicsDruid::GraphicsDruid(void) { this->reserved = 0; } GraphicsDruid::GraphicsDruid(void) { this->reserved = 0; }
@ -178,7 +180,7 @@ int GraphicsDruid::loadTranspTexture(char *path, float *transpColor, int id) {
(Uint8)(transpColor[1] * 255), (Uint8)(transpColor[2] * 255)); (Uint8)(transpColor[1] * 255), (Uint8)(transpColor[2] * 255));
// SDL_SetAlpha(texture, 0, SDL_ALPHA_OPAQUE); // SDL_SetAlpha(texture, 0, SDL_ALPHA_OPAQUE);
SDL_SetColorKey(texture, SDL_SRCCOLORKEY, colorKey); SDL_SetColorKey(texture, SDL_TRUE, colorKey);
// SDL_Surface* alphaSurface = SDL_DisplayFormatAlpha(texture); // SDL_Surface* alphaSurface = SDL_DisplayFormatAlpha(texture);
texture = SDL_DisplayFormatAlpha(texture); texture = SDL_DisplayFormatAlpha(texture);

View file

@ -17,9 +17,9 @@
#include "glapi.h" #include "glapi.h"
SDL_Window *window;
int screenwidth = 1024; int screenwidth = 1024;
int screenheight = 768; int screenheight = 768;
int screenbpp = 32;
// static float speedfactor = 1.0; // static float speedfactor = 1.0;
@ -36,12 +36,19 @@ void exitProgram(int code) {
} }
void changeResolution(int width, int height, bool fullscreen) { void changeResolution(int width, int height, bool fullscreen) {
int mode = SDL_OPENGL; int mode = SDL_WINDOW_OPENGL;
if (fullscreen) if (fullscreen)
mode |= SDL_FULLSCREEN; mode |= SDL_WINDOW_FULLSCREEN;
if (!SDL_SetVideoMode(width, height, screenbpp, mode)) {
fprintf(stderr, "Couldn't set %i*%i*%i opengl video mode: %s\n", window = SDL_CreateWindow("BlockoFighter 2", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, mode);
screenwidth, screenheight, screenbpp, SDL_GetError()); 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); exitProgram(-1);
} }
@ -58,11 +65,20 @@ void changeResolution(int width, int height, bool fullscreen) {
initScenes(); initScenes();
} }
bool keys[SDLK_LAST] = {false}; std::set<SDL_Keycode> keys;
void handleKeydown(SDL_keysym *keysym) { keys[keysym->sym] = true; } void handleKeydown(SDL_Keysym *keysym) {
if (!keys.count(keysym->sym)) {
keys.insert(keysym->sym);
}
}
void handleKeyup(SDL_keysym *keysym) { keys[keysym->sym] = false; } void handleKeyup(SDL_Keysym *keysym) {
auto it = keys.find(keysym->sym);
if (it != keys.end()) {
keys.erase(it);
}
}
void processEvents(void) { void processEvents(void) {
SDL_Event event; SDL_Event event;
@ -74,9 +90,9 @@ void processEvents(void) {
case SDL_KEYUP: case SDL_KEYUP:
handleKeyup(&event.key.keysym); handleKeyup(&event.key.keysym);
break; break;
case SDL_VIDEORESIZE: case SDL_WINDOWEVENT_RESIZED:
screenwidth = event.resize.w; screenwidth = event.window.data1;
screenheight = event.resize.h; screenheight = event.window.data2;
setupOpengl(screenwidth, screenheight); setupOpengl(screenwidth, screenheight);
break; break;
case SDL_QUIT: case SDL_QUIT:
@ -106,12 +122,11 @@ int main(int argc, char *argv[]) {
exitProgram(-1); exitProgram(-1);
} }
const SDL_VideoInfo *info = SDL_GetVideoInfo(); /*const SDL_VideoInfo *info = SDL_GetVideoInfo();
if (!info) { if (!info) {
printf("Could not get video info with SDL: %s.\n", SDL_GetError()); printf("Could not get video info with SDL: %s.\n", SDL_GetError());
exitProgram(-1); exitProgram(-1);
} }*/
screenbpp = info->vfmt->BitsPerPixel;
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5); SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5); SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5); SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
@ -120,8 +135,6 @@ int main(int argc, char *argv[]) {
atexit(SDL_Quit); atexit(SDL_Quit);
SDL_WM_SetCaption("BlockoFighter 2", NULL);
initAudio(); initAudio();
changeResolution(1024, 768, false); changeResolution(1024, 768, false);

View file

@ -8,6 +8,7 @@
disable : 4305) // Disable: truncation from 'const double' to 'double' disable : 4305) // Disable: truncation from 'const double' to 'double'
#endif #endif
#include <set>
#include "SDL.h" #include "SDL.h"
#ifdef DEBUG #ifdef DEBUG
@ -25,7 +26,8 @@
#endif #endif
#define DATAPATH "data/" #define DATAPATH "data/"
extern bool keys[SDLK_LAST]; extern std::set<SDL_Keycode> keys; // TODO move in proper place
extern SDL_Window *window;
void exitProgram(int code); void exitProgram(int code);
void changeResolution(int width, int height, bool fullscreen); void changeResolution(int width, int height, bool fullscreen);

View file

@ -469,7 +469,7 @@ void calculateMenu(int framecount) {
} else { } else {
interpolator = 1.0; interpolator = 1.0;
if (menufade == -1) { if (menufade == -1) {
if (keys[SDLK_DOWN]) { if (keys.count(SDLK_DOWN)) {
if (!pressed) { if (!pressed) {
/*if (menuoption < maxoption){ /*if (menuoption < maxoption){
menuoption++; menuoption++;
@ -479,7 +479,7 @@ void calculateMenu(int framecount) {
changesound->play(); changesound->play();
pressed = true; pressed = true;
} }
} else if (keys[SDLK_UP]) { } else if (keys.count(SDLK_UP)) {
if (!pressed) { if (!pressed) {
/*if (menuoption > 0){ /*if (menuoption > 0){
menuoption--; menuoption--;
@ -489,7 +489,7 @@ void calculateMenu(int framecount) {
changesound->play(); changesound->play();
pressed = true; pressed = true;
} }
} else if (keys[SDLK_LEFT]) { } else if (keys.count(SDLK_LEFT)) {
if (!pressed) { if (!pressed) {
switch (menumode) { switch (menumode) {
case MODEOPTIONS: case MODEOPTIONS:
@ -507,7 +507,7 @@ void calculateMenu(int framecount) {
selectsound->play(); selectsound->play();
pressed = true; pressed = true;
} }
} else if (keys[SDLK_RIGHT]) { } else if (keys.count(SDLK_RIGHT)) {
if (!pressed) { if (!pressed) {
switch (menumode) { switch (menumode) {
case MODEOPTIONS: case MODEOPTIONS:
@ -525,7 +525,7 @@ void calculateMenu(int framecount) {
selectsound->play(); selectsound->play();
pressed = true; pressed = true;
} }
} else if (keys[SDLK_ESCAPE]) { } else if (keys.count(SDLK_ESCAPE)) {
if (!pressed) { if (!pressed) {
switch (menumode) { switch (menumode) {
case MODEMAIN: case MODEMAIN:
@ -544,7 +544,7 @@ void calculateMenu(int framecount) {
} }
pressed = true; pressed = true;
} }
} else if (keys[SDLK_RETURN]) { } else if (keys.count(SDLK_RETURN)) {
if (!pressed) { if (!pressed) {
switch (menumode) { switch (menumode) {
case MODEMAIN: case MODEMAIN:

View file

@ -30,13 +30,13 @@ void initScenes(void) {
enable2D(); enable2D();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
SDL_GL_SwapBuffers(); SDL_GL_SwapWindow(window);
initFontTexture(); initFontTexture();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
print(0.08, 0.4, "Loading...", 0.2); print(0.08, 0.4, "Loading...", 0.2);
SDL_GL_SwapBuffers(); SDL_GL_SwapWindow(window);
disable2D(); disable2D();
@ -92,7 +92,7 @@ void drawFrame(int framecount) {
break; break;
} }
SDL_GL_SwapBuffers(); SDL_GL_SwapWindow(window);
} }
/*int SKY_FRONT_ID; /*int SKY_FRONT_ID;

View file

@ -1,6 +1,12 @@
#include "texture.h" #include "texture.h"
#include "graphics.h" #include "graphics.h"
SDL_Surface *SDL_DisplayFormatAlpha(SDL_Surface *surf) {
SDL_Surface *alpha_surf = SDL_ConvertSurfaceFormat(surf, SDL_PIXELFORMAT_ARGB8888, 0);
SDL_FreeSurface(surf);
return alpha_surf;
}
Texture::Texture(void) { Texture::Texture(void) {
this->textureId = -1; this->textureId = -1;
this->enabled = false; this->enabled = false;
@ -47,7 +53,7 @@ bool Texture::loadImage(char *path, float *trans) {
(Uint8)(trans[1] * 255), (Uint8)(trans[2] * 255)); (Uint8)(trans[1] * 255), (Uint8)(trans[2] * 255));
// SDL_SetAlpha(texture, 0, SDL_ALPHA_OPAQUE); // SDL_SetAlpha(texture, 0, SDL_ALPHA_OPAQUE);
SDL_SetColorKey(texture, SDL_SRCCOLORKEY, colorKey); SDL_SetColorKey(texture, SDL_TRUE, colorKey);
// SDL_Surface* alphaSurface = SDL_DisplayFormatAlpha(texture); // SDL_Surface* alphaSurface = SDL_DisplayFormatAlpha(texture);
texture = SDL_DisplayFormatAlpha(texture); texture = SDL_DisplayFormatAlpha(texture);

View file

@ -32,4 +32,6 @@ private:
int format; int format;
}; };
SDL_Surface *SDL_DisplayFormatAlpha(SDL_Surface *surf);
#endif #endif