-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
162 lines (133 loc) · 4.58 KB
/
main.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#define SDL_MAIN_HANDLED
#include <SDL.h>
#include <stdio.h>
#include "Context.h"
#include "Game.h"
#include "Graphics.h"
#include "Math.h"
#include "DeltaTime.h"
#include "Input.h"
#include <stdlib.h>
#include <SDL_image.h>
#include <time.h>
#define SCREEN_WIDTH 1024
#define SCREEN_HEIGHT 768
int main( int argc, char* args[] ) {
srand(time(NULL));
//Keep track of the frame count
int frame = 0;
//The window we'll be rendering to
SDL_Window* window = NULL;
//The surface contained by the window
SDL_Surface* screenSurface = NULL;
//Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
}
else
{
//Create window
window = SDL_CreateWindow( "B2Engine", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
if( window == NULL )
{
printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
}
else
{
//Create renderer
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
//Create game context
Context* game = (Context*)malloc(sizeof(Context));
//Initialize context
initContext(game);
//Show loaded sprites
showSpritesLoaded();
//FPS Counter
int numFrames = 0;
uint32_t startTime = SDL_GetTicks();
//Start Delta Time
initializeTime();
//Add player
Sprite sprite;
loadSprite(renderer, &sprite, "player.png");
EntityID id = addEntity(
game,
20,
20,
sprite.width, sprite.height,
0,
&sprite,
SOLID
);
addVelocityComponent(
game,
0,
0,
id
);
addInputListener(
game,
true,
id
);
//Add Template entity
Sprite tempSprite;
loadSprite(renderer, &tempSprite, "gold.png");
EntityID templateId = addEntity(
game,
randomBetween(0, SCREEN_WIDTH - sprite.height),
randomBetween(0, SCREEN_HEIGHT - sprite.width),
tempSprite.width, tempSprite.height,
(float)randomBetween(0, 360),
&tempSprite,
SOLID
);
//Clone that entity few more times.
for(uint8_t i = 0; i < 9; ++i){
EntityID cloneId = cloneEntity(game, templateId);
setPosition(game, cloneId, randomBetween(0, SCREEN_WIDTH - sprite.height), randomBetween(0, SCREEN_HEIGHT - sprite.width));
}
//Game loop
while(true){
++numFrames;
Uint32 elapsedMS = SDL_GetTicks() - startTime;
if (elapsedMS) { // Skip this the first frame
double elapsedSeconds = elapsedMS / 1000.0; // Convert to seconds
double fps = numFrames / elapsedSeconds; // FPS is Frames / Seconds
char buffer[512];
sprintf(buffer, "B2 Engine - FPS: %d\n", (int)fps);
SDL_SetWindowTitle(window, buffer);
}
tickDeltaTime();
SDL_Event event;
if (SDL_PollEvent(&event)) {
input.keyReleased = 0;
if (event.type == SDL_QUIT) {
// Break out of the loop on quit
break;
}
if(event.type == SDL_KEYDOWN){
input.keyPressed = event.key.keysym.sym;
}
if(event.type == SDL_KEYUP){
input.keyPressed = 0;
input.keyReleased = event.key.keysym.sym;
}
}
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
updateEntities(game);
drawEntities(renderer, game);
SDL_RenderPresent(renderer);
}
}
}
freeTime();
destroyTextureLibrary();
//Destroy window
SDL_DestroyWindow(window);
//Quit SDL subsystems
SDL_Quit();
return 0;
}