]> git.r.bdr.sh - rbdr/super-polarity/blobdiff - src/main.c
Add some basic actorstuffs
[rbdr/super-polarity] / src / main.c
index 97d1e38facb0e054e792dff023ba7ce5f343f534..ae73a22f2177bfe4cdf70ebe81ed9cfcc9270bd7 100644 (file)
 
 #include <stdio.h>
 #include <time.h>
+#include <math.h>
 
 #include "SDL2/SDL.h"
+#include "SDL2_image/SDL_image.h"
+
+#include "actor.h"
+#include "actor_manager.h"
 
 // TODO: Move these guys to a config header file
 #define SCREEN_WIDTH 640
 #define SCREEN_HEIGHT 480
 #define FPS 30
 
-int main(int argc, const char * argv[])
-{
-    
-    // SDL Initialization. TODO: Should have an initializer.
-    SDL_Window *window;
-    SDL_Renderer *renderer;
-    Uint32 startFrame;
-    Uint32 endFrame;
-    Uint32 delay;
-    int done;
-    
+SDL_Window *window;
+SDL_Renderer *renderer;
+SDL_Texture *benHead;
+SDL_Rect benRect;
+bool focus;
+
+ActorManager *actorManager;
+
+void init () {
     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
         printf("Could not initialize SDL");
         exit(1);
     }
     
-    window = SDL_CreateWindow("Super Polarity", 50, 50, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_OPENGL | SDL_WINDOW_BORDERLESS);
+    // Load the window and renderer
+    window = SDL_CreateWindow("Super Polarity", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 0, 0, SDL_WINDOW_OPENGL | SDL_WINDOW_FULLSCREEN_DESKTOP);
     
     renderer = SDL_CreateRenderer(window, -1, 0);
     
+    // Set linear quality for scaling, and the "logical" window size.
+    SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");
+    SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH, SCREEN_HEIGHT);
+}
+
+void close () {
+    SDL_DestroyTexture(benHead);
+    SDL_DestroyRenderer(renderer);
+    SDL_DestroyWindow(window);
+    SDL_Quit();
+}
+
+
+void initActorManager () {
+    Actor *actor;
+    Actor *otherActor;
+    
+    //test the Actor Manager.
+    actorManager = createActorManager();
+    
+    actor = createActor();
+    actor->pos.x = 55;
+    actor->pos.y = 24;
+    actorManager->addActor(actorManager, actor);
+    
+    otherActor = createActor();
+    otherActor->pos.x=100;
+    otherActor->pos.y=100;
+    actorManager->addActor(actorManager, otherActor);
+    
+}
+
+void loadAssets() {
+    initActorManager();
+}
+
+void draw (SDL_Renderer *renderer) {
+    SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
+    SDL_RenderClear(renderer);
+    
+    actorManager->draw(actorManager);
+    
+    SDL_RenderPresent(renderer);
+}
+
+void update (Uint32 dt) {
+    actorManager->update(actorManager, dt);
+}
+
+int main(int argc, const char * argv[])
+{
+    
+    // SDL Initialization. TODO: Should have an initializer.
+    Uint32 startFrame = 0;
+    Uint32 endFrame = 0;
+    Uint32 lastTime = 0;
+    Uint32 dt;
+    Uint32 delay;
+    int done;
+    
+    init();
+    loadAssets();
+   
+    
     done = 0;
+    focus = true;
     
     // Event Loop.
     while (!done) {
         startFrame = SDL_GetTicks();
+        dt = startFrame - lastTime;
+        lastTime = startFrame;
+        
         SDL_Event event;
         while (SDL_PollEvent(&event)) {
             if (event.type == SDL_QUIT) {
                 done = 1;
             }
+            if (event.type == SDL_WINDOWEVENT) {
+                if (event.window.event == SDL_WINDOWEVENT_FOCUS_LOST) {
+                    focus = false;
+                }
+                if (event.window.event == SDL_WINDOWEVENT_FOCUS_GAINED) {
+                    focus = true;
+                }
+            }
+        }
+        
+        if (focus) {
+            update(dt);
+            draw(renderer);
         }
         
         endFrame = SDL_GetTicks();
@@ -58,7 +143,7 @@ int main(int argc, const char * argv[])
         SDL_Delay(delay);
     }
     
-    SDL_Quit();
+    close();
 
     return 0;
 }