]>
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 BB |
15 | |
16 | // TODO: Move these guys to a config header file | |
17 | #define SCREEN_WIDTH 640 | |
18 | #define SCREEN_HEIGHT 480 | |
19 | #define FPS 30 | |
20 | ||
2a85937c BB |
21 | SDL_Window *window; |
22 | SDL_Renderer *renderer; | |
ed603039 BB |
23 | SDL_Texture *benHead; |
24 | SDL_Rect benRect; | |
25 | double angle = 0; | |
2a85937c BB |
26 | |
27 | void init () { | |
7407ac7f BB |
28 | if (SDL_Init(SDL_INIT_VIDEO) < 0) { |
29 | printf("Could not initialize SDL"); | |
30 | exit(1); | |
31 | } | |
32 | ||
2a85937c BB |
33 | // Load the window and renderer |
34 | window = SDL_CreateWindow("Super Polarity", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 0, 0, SDL_WINDOW_OPENGL | SDL_WINDOW_FULLSCREEN_DESKTOP); | |
7407ac7f BB |
35 | |
36 | renderer = SDL_CreateRenderer(window, -1, 0); | |
37 | ||
2a85937c BB |
38 | // Set linear quality for scaling, and the "logical" window size. |
39 | SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear"); | |
40 | SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH, SCREEN_HEIGHT); | |
ed603039 BB |
41 | } |
42 | ||
43 | void close () { | |
44 | SDL_DestroyTexture(benHead); | |
45 | SDL_DestroyRenderer(renderer); | |
46 | SDL_DestroyWindow(window); | |
47 | SDL_Quit(); | |
48 | } | |
49 | ||
50 | void loadAssets() { | |
2a85937c | 51 | // Load the surface. |
ed603039 BB |
52 | SDL_Surface *benSurface; |
53 | benSurface = IMG_Load("data/img/static/ben.png"); | |
54 | benHead = SDL_CreateTextureFromSurface(renderer, benSurface); | |
55 | benRect.x = 230; | |
56 | benRect.y = 150; | |
57 | benRect.w = 180; | |
58 | benRect.h = 180; | |
59 | SDL_FreeSurface(benSurface); | |
2a85937c BB |
60 | } |
61 | ||
62 | void render (SDL_Renderer *renderer) { | |
63 | SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); | |
64 | SDL_RenderClear(renderer); | |
ed603039 | 65 | SDL_RenderCopyEx(renderer, benHead, NULL, &benRect, angle, NULL, SDL_FLIP_NONE); |
2a85937c BB |
66 | SDL_RenderPresent(renderer); |
67 | } | |
68 | ||
ed603039 BB |
69 | void update (dt) { |
70 | angle = (Uint32)(angle + dt / 5) % 360; | |
71 | } | |
72 | ||
73 | int main(int argc, const char * argv[]) | |
2a85937c BB |
74 | { |
75 | ||
76 | // SDL Initialization. TODO: Should have an initializer. | |
ed603039 BB |
77 | Uint32 startFrame = 0; |
78 | Uint32 endFrame = 0; | |
79 | Uint32 lastTime = 0; | |
80 | Uint32 dt; | |
2a85937c BB |
81 | Uint32 delay; |
82 | int done; | |
ed603039 BB |
83 | |
84 | init(); | |
85 | loadAssets(); | |
2a85937c BB |
86 | |
87 | ||
7407ac7f BB |
88 | done = 0; |
89 | ||
90 | // Event Loop. | |
91 | while (!done) { | |
92 | startFrame = SDL_GetTicks(); | |
ed603039 BB |
93 | dt = startFrame - lastTime; |
94 | lastTime = startFrame; | |
95 | ||
7407ac7f BB |
96 | SDL_Event event; |
97 | while (SDL_PollEvent(&event)) { | |
98 | if (event.type == SDL_QUIT) { | |
99 | done = 1; | |
100 | } | |
101 | } | |
102 | ||
ed603039 | 103 | update(dt); |
2a85937c BB |
104 | render(renderer); |
105 | ||
7407ac7f BB |
106 | endFrame = SDL_GetTicks(); |
107 | ||
108 | /* see if we have time to sleep */ | |
109 | delay = 1000 / FPS - (endFrame - startFrame); | |
110 | if (delay > 1000 / FPS) { | |
111 | delay = 1000 / FPS; | |
112 | } | |
113 | SDL_Delay(delay); | |
114 | } | |
115 | ||
ed603039 | 116 | close(); |
7407ac7f BB |
117 | |
118 | return 0; | |
119 | } | |
120 |