]>
Commit | Line | Data |
---|---|---|
7407ac7f BB |
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> | |
ed603039 | 11 | #include <math.h> |
7407ac7f BB |
12 | |
13 | #include "SDL2/SDL.h" | |
2a85937c | 14 | #include "SDL2_image/SDL_image.h" |
7407ac7f | 15 | |
8534e46e BB |
16 | #include "actor.h" |
17 | #include "actor_manager.h" | |
18 | ||
7407ac7f BB |
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 | ||
2a85937c BB |
24 | SDL_Window *window; |
25 | SDL_Renderer *renderer; | |
ed603039 BB |
26 | SDL_Texture *benHead; |
27 | SDL_Rect benRect; | |
8534e46e BB |
28 | bool focus; |
29 | ||
30 | ActorManager *actorManager; | |
2a85937c BB |
31 | |
32 | void init () { | |
7407ac7f BB |
33 | if (SDL_Init(SDL_INIT_VIDEO) < 0) { |
34 | printf("Could not initialize SDL"); | |
35 | exit(1); | |
36 | } | |
37 | ||
2a85937c BB |
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); | |
7407ac7f BB |
40 | |
41 | renderer = SDL_CreateRenderer(window, -1, 0); | |
42 | ||
2a85937c BB |
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); | |
ed603039 BB |
46 | } |
47 | ||
48 | void close () { | |
49 | SDL_DestroyTexture(benHead); | |
50 | SDL_DestroyRenderer(renderer); | |
51 | SDL_DestroyWindow(window); | |
52 | SDL_Quit(); | |
53 | } | |
54 | ||
8534e46e BB |
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 | ||
ed603039 | 75 | void loadAssets() { |
8534e46e | 76 | initActorManager(); |
2a85937c BB |
77 | } |
78 | ||
8534e46e | 79 | void draw (SDL_Renderer *renderer) { |
2a85937c BB |
80 | SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); |
81 | SDL_RenderClear(renderer); | |
8534e46e BB |
82 | |
83 | actorManager->draw(actorManager); | |
84 | ||
2a85937c BB |
85 | SDL_RenderPresent(renderer); |
86 | } | |
87 | ||
8534e46e BB |
88 | void update (Uint32 dt) { |
89 | actorManager->update(actorManager, dt); | |
ed603039 BB |
90 | } |
91 | ||
92 | int main(int argc, const char * argv[]) | |
2a85937c BB |
93 | { |
94 | ||
95 | // SDL Initialization. TODO: Should have an initializer. | |
ed603039 BB |
96 | Uint32 startFrame = 0; |
97 | Uint32 endFrame = 0; | |
98 | Uint32 lastTime = 0; | |
99 | Uint32 dt; | |
2a85937c BB |
100 | Uint32 delay; |
101 | int done; | |
ed603039 BB |
102 | |
103 | init(); | |
104 | loadAssets(); | |
2a85937c BB |
105 | |
106 | ||
7407ac7f | 107 | done = 0; |
8534e46e | 108 | focus = true; |
7407ac7f BB |
109 | |
110 | // Event Loop. | |
111 | while (!done) { | |
112 | startFrame = SDL_GetTicks(); | |
ed603039 BB |
113 | dt = startFrame - lastTime; |
114 | lastTime = startFrame; | |
115 | ||
7407ac7f BB |
116 | SDL_Event event; |
117 | while (SDL_PollEvent(&event)) { | |
118 | if (event.type == SDL_QUIT) { | |
119 | done = 1; | |
120 | } | |
8534e46e BB |
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 | } | |
7407ac7f BB |
129 | } |
130 | ||
8534e46e BB |
131 | if (focus) { |
132 | update(dt); | |
133 | draw(renderer); | |
134 | } | |
2a85937c | 135 | |
7407ac7f BB |
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 | ||
ed603039 | 146 | close(); |
7407ac7f BB |
147 | |
148 | return 0; | |
149 | } | |
150 |