]> git.r.bdr.sh - rbdr/super-polarity/blame_incremental - src/main.c
Rotation!
[rbdr/super-polarity] / src / main.c
... / ...
CommitLineData
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// TODO: Move these guys to a config header file
17#define SCREEN_WIDTH 640
18#define SCREEN_HEIGHT 480
19#define FPS 30
20
21SDL_Window *window;
22SDL_Renderer *renderer;
23SDL_Texture *benHead;
24SDL_Rect benRect;
25double angle = 0;
26
27void init () {
28 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
29 printf("Could not initialize SDL");
30 exit(1);
31 }
32
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);
35
36 renderer = SDL_CreateRenderer(window, -1, 0);
37
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);
41}
42
43void close () {
44 SDL_DestroyTexture(benHead);
45 SDL_DestroyRenderer(renderer);
46 SDL_DestroyWindow(window);
47 SDL_Quit();
48}
49
50void loadAssets() {
51 // Load the surface.
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);
60}
61
62void render (SDL_Renderer *renderer) {
63 SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
64 SDL_RenderClear(renderer);
65 SDL_RenderCopyEx(renderer, benHead, NULL, &benRect, angle, NULL, SDL_FLIP_NONE);
66 SDL_RenderPresent(renderer);
67}
68
69void update (dt) {
70 angle = (Uint32)(angle + dt / 5) % 360;
71}
72
73int main(int argc, const char * argv[])
74{
75
76 // SDL Initialization. TODO: Should have an initializer.
77 Uint32 startFrame = 0;
78 Uint32 endFrame = 0;
79 Uint32 lastTime = 0;
80 Uint32 dt;
81 Uint32 delay;
82 int done;
83
84 init();
85 loadAssets();
86
87
88 done = 0;
89
90 // Event Loop.
91 while (!done) {
92 startFrame = SDL_GetTicks();
93 dt = startFrame - lastTime;
94 lastTime = startFrame;
95
96 SDL_Event event;
97 while (SDL_PollEvent(&event)) {
98 if (event.type == SDL_QUIT) {
99 done = 1;
100 }
101 }
102
103 update(dt);
104 render(renderer);
105
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
116 close();
117
118 return 0;
119}
120