forked from Drabble/SuperMario
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.c
120 lines (107 loc) · 3.25 KB
/
game.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
Super Mario Bros
Game.c
Antoine Drabble
Création: 07.01.13
*/
#include <stdlib.h>
#include <stdio.h>
#include <SDL.h>
#include <SDL_image.h>
#include "game.h"
#include "map.h"
#include "char.h"
#include "object.h"
#include "enemy.h"
#include "objectevent.h"
#include "enemyevent.h"
#include "event.h"
#include "audio.h"
void game(SDL_Surface* screen)
{
SDL_Event event;
Map* m;
Sprites* S;
Chars mario;
Chars* marioimages;
Object* shroom;
Enemy* enemy;
Input in;
int keepGoing = 0;
int previousTime = 0, currentTime = 0;
char *theLevel;
marioimages = malloc(12 * sizeof(Chars));
memset(&in,0,sizeof(in)); //Set all keys to 0
SDL_Rect positionLevel;
SDL_Surface* level;
screen = SDL_SetVideoMode(600, 600, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
level = IMG_Load("images/lvl.jpg"); //Load menu image
positionLevel.x = screen->w / 2 - level->w / 2; //Sets the menu to the center of the window
positionLevel.y = 0;
SDL_BlitSurface(level, NULL, screen, &positionLevel);
SDL_Flip(screen);
SDL_FreeSurface(level);
while (!keepGoing)
{
SDL_WaitEvent(&event);
switch(event.type)
{
case SDL_KEYDOWN:
switch(event.key.keysym.sym)
{
case SDLK_ESCAPE:
return;
break;
case SDLK_1:
theLevel="theLevel1.lvl";
keepGoing=1;
break;
case SDLK_2:
theLevel="theLevel2.lvl";
keepGoing=1;
break;
case SDLK_3:
theLevel="theLevel3.lvl";
keepGoing=1;
break;
}
break;
}
}
screen = SDL_SetVideoMode( WindowW, WindowH, 32,SDL_HWSURFACE|SDL_DOUBLEBUF);
keepGoing=0;
S = LoadImages();
shroom = LoadObject();
m = LoadMap(theLevel);
enemy = LoadEnemy(m);
LoadChars(&mario, m, marioimages);
ShowMap(m, screen, S);
SDL_Flip(screen);
while(!in.key[SDLK_ESCAPE] && !in.quit && !keepGoing)// simplification of key management
{
currentTime = SDL_GetTicks();
if (currentTime - previousTime > 4) /* If 4 ms have elapsed since the last loop round */
{
UpdateEvents(&in);
MapScroll(m, &mario);
Evolve(&in,m,&mario,S, shroom);
ShowMap(m,screen,S);
objectmove(shroom, &mario, m->xscroll,m->yscroll, m, S);
ShowObject(screen, shroom, m->xscroll,m->yscroll);
enemymove(enemy, &mario, m->xscroll,m->yscroll, m, S);
ShowEnemy(screen, enemy, m->xscroll,m->yscroll);
ShowPerson(&mario,screen,m->xscroll,m->yscroll, marioimages);
SDL_Flip(screen);
keepGoing = FinishLevel(screen, &mario, m);
previousTime = currentTime; /* The "current" time becomes the "previous" time for our future calculations */
}
else
{
SDL_Delay(4 - (currentTime - previousTime));
}
}
FreeMap(m,S);
FreeChars(&mario, marioimages);
FreeObject(shroom);
FreeEnemy(enemy);
}