There is my little project, where I've tried to use all abilities of SDL framework.
After many hours spent reading The Hobbit, The Witcher and Lord of the Rings, I decided to play out my fantasy and put it to code. So, I'm proud to show you my medieval-themed wandering game. Mages, knights, dwarves and more! Explore, check it out and find out! I wrote the game in pure C, using SDL, based on the base. Music, graphics, plot and so on - all my own!
Now I will show you what you can do in my game
- There you can read some info about the game, sadly at this point I don't add pretty much to read, but it will appear in the nearest future!
info_video.mp4
- There you can try to play for the wizard! I still don't add some features and gameplays moment, but at least you can test some of them!
test_wiz.mp4
play_wiz.mp4
- And there you can try yourself as a knight!
test_knight.mp4
play_knight.mp4
- If you want to end your game you will see something like that
lose_video.mp4
- Crawly The Gnome
- Stopiaż z Bałut
- Project Dupa
Project created with:
- SDL for most features
- SDL_mixer for music
- SDL_image for correcting photoes
- SDL_ttf for text usage
- Photoshop for editing photoes
- Tavern music and beer
First we declare some important constants, window size and player position.
The interesting thing is that the level switching is done via the enum construct, which makes it much easier to switch from one screen to another:
typedef enum
{
MENU,
WIKI,
PICK,
GAME,
TEST,
OVER
} GameState;
Next comes a very long and crookedly done initialisation of photos, texts, sounds and objects, and of course all this should have been put out separately and more beautifully, but! I didn't do it :(
Then again the declaration of variables for our character's animations and finally the algorithm for generating the background. Initially we create a table, filling it with values that are later responsible for a certain colour:
int table[100][60] = { 0 };
for(int i = 0; i < 100; i += 4)
{
int temp = -2 + rand() % 4;
int effective_height = max_height + temp;
for(int j = 0; j < 60; j += 4)
{
int value = 1 + rand() % 4;
for (int bi = 0; bi < 4 && i + bi < 100; bi++)
{
for (int bj = 0; bj < 4 && j + bj < 60; bj++)
{
if (j + bj < effective_height)
{
table[i + bi][j + bj] = 0;
}
else
{
table[i + bi][j + bj] = value;
}
}
}
}
}
Once the game loop starts, we just handle some keystroke events moving the player, changing and scrolling animations. Then, depending on the player's action, we initialise our variables indicating running, standing, attacking or whatever, so that we can understand what animation we should include.
Then we clear the window and start displaying our windows, which are stored in our enum, via switch. Then we play the desired animation, and the cycle repeats while we play.
Then we clear all arrays, because in SDL it is not done automatically )). Now about the file handler_of_screen.c, function that draws the background, we divide the whole screen into small squares, which, depending on the value in the table, we fill with a certain colour. The most important, the function that loads textures into the array, we run through all the frames of the animation, trying to load the file and convert it into a texture that we can already show:
int loadTextureArray(SDL_Renderer* renderer, const char** fileNames, int arraySize, SDL_Texture** textureArray)
{
for(int i = 0; i < arraySize; i++)
{
SDL_Surface* surface = SDL_LoadBMP(fileNames[i]);
if (surface == NULL)
{
printf("Error loading image %d: %s\n", i, SDL_GetError());
SDL_DestroyRenderer(renderer);
return 1;
}
textureArray[i] = SDL_CreateTextureFromSurface(renderer, surface);
SDL_FreeSurface(surface);
if (textureArray[i] == NULL)
{
printf("Error creating texture for image %d: %s\n", i, SDL_GetError());
SDL_DestroyRenderer(renderer);
return 1;
}
}
return 0;
}
Next are the functions of drawing different screens, there is nothing unusual there, at the end we have a function that checks the intersection of two objects:
int is_intersects(SDL_Rect rect1, SDL_Rect rect2, int pick)
{
if (pick == 0)
{
if (rect2.x >= rect1.x - 130 && rect2.x <= rect1.x)
{
return 1;
}
}
else if(pick == 1)
{
if(rect2.x >= rect1.x && rect2.x <= rect1.x + 140)
{
return 1;
}
}
return 0;
}
All in all, this is an ending
Just download the recent release