]> git.r.bdr.sh - rbdr/super-polarity/blob - src/main.c
Proper merge for gitignore
[rbdr/super-polarity] / src / main.c
1 //
2 // main.c
3 // Super Polarity
4 //
5 // Created by Ruben Beltran del Rio on 8/13/13.
6 // Copyright (c) 2013 Abuguet. All rights reserved.
7 //
8
9 #include <stdio.h>
10 #include <time.h>
11 #include <math.h>
12
13 #include "SDL2/SDL.h"
14 #include "SDL2_image/SDL_image.h"
15
16 #include "actor.h"
17 #include "actor_manager.h"
18
19 // TODO: Move these guys to a config header file
20 #define SCREEN_WIDTH 640
21 #define SCREEN_HEIGHT 480
22 #define FPS 30
23
24 SDL_Window *window;
25 SDL_Renderer *renderer;
26 SDL_Texture *benHead;
27 SDL_Rect benRect;
28 bool focus;
29
30 ActorManager *actorManager;
31
32 void init () {
33 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
34 printf("Could not initialize SDL");
35 exit(1);
36 }
37
38 // Load the window and renderer
39 window = SDL_CreateWindow("Super Polarity", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 0, 0, SDL_WINDOW_OPENGL | SDL_WINDOW_FULLSCREEN_DESKTOP);
40
41 renderer = SDL_CreateRenderer(window, -1, 0);
42
43 // Set linear quality for scaling, and the "logical" window size.
44 SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");
45 SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH, SCREEN_HEIGHT);
46 }
47
48 void close () {
49 SDL_DestroyTexture(benHead);
50 SDL_DestroyRenderer(renderer);
51 SDL_DestroyWindow(window);
52 SDL_Quit();
53 }
54
55
56 void initActorManager () {
57 Actor *actor;
58 Actor *otherActor;
59
60 //test the Actor Manager.
61 actorManager = createActorManager();
62
63 actor = createActor();
64 actor->pos.x = 55;
65 actor->pos.y = 24;
66 actorManager->addActor(actorManager, actor);
67
68 otherActor = createActor();
69 otherActor->pos.x=100;
70 otherActor->pos.y=100;
71 actorManager->addActor(actorManager, otherActor);
72
73 }
74
75 void loadAssets() {
76 initActorManager();
77 }
78
79 void draw (SDL_Renderer *renderer) {
80 SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
81 SDL_RenderClear(renderer);
82
83 actorManager->draw(actorManager);
84
85 SDL_RenderPresent(renderer);
86 }
87
88 void update (Uint32 dt) {
89 actorManager->update(actorManager, dt);
90 }
91
92 int main(int argc, const char * argv[])
93 {
94
95 // SDL Initialization. TODO: Should have an initializer.
96 Uint32 startFrame = 0;
97 Uint32 endFrame = 0;
98 Uint32 lastTime = 0;
99 Uint32 dt;
100 Uint32 delay;
101 int done;
102
103 init();
104 loadAssets();
105
106
107 done = 0;
108 focus = true;
109
110 // Event Loop.
111 while (!done) {
112 startFrame = SDL_GetTicks();
113 dt = startFrame - lastTime;
114 lastTime = startFrame;
115
116 SDL_Event event;
117 while (SDL_PollEvent(&event)) {
118 if (event.type == SDL_QUIT) {
119 done = 1;
120 }
121 if (event.type == SDL_WINDOWEVENT) {
122 if (event.window.event == SDL_WINDOWEVENT_FOCUS_LOST) {
123 focus = false;
124 }
125 if (event.window.event == SDL_WINDOWEVENT_FOCUS_GAINED) {
126 focus = true;
127 }
128 }
129 }
130
131 if (focus) {
132 update(dt);
133 draw(renderer);
134 }
135
136 endFrame = SDL_GetTicks();
137
138 /* see if we have time to sleep */
139 delay = 1000 / FPS - (endFrame - startFrame);
140 if (delay > 1000 / FPS) {
141 delay = 1000 / FPS;
142 }
143 SDL_Delay(delay);
144 }
145
146 close();
147
148 return 0;
149 }
150