]> git.r.bdr.sh - rbdr/super-polarity/commitdiff
Add some basic actorstuffs
authorBen Beltran <redacted>
Wed, 14 Aug 2013 14:00:22 +0000 (09:00 -0500)
committerBen Beltran <redacted>
Wed, 14 Aug 2013 14:00:22 +0000 (09:00 -0500)
Super Polarity.xcodeproj/project.pbxproj
Super Polarity.xcodeproj/project.xcworkspace/xcuserdata/benbeltran.xcuserdatad/UserInterfaceState.xcuserstate
Super Polarity.xcodeproj/xcuserdata/benbeltran.xcuserdatad/xcdebugger/Breakpoints.xcbkptlist [new file with mode: 0644]
src/actor.c [new file with mode: 0644]
src/actor.h [new file with mode: 0644]
src/actor_manager.c [new file with mode: 0644]
src/actor_manager.h [new file with mode: 0644]
src/main.c

index 364ccf7ad8f30821dcb0295ea45285ca1d15f566..b39b0e24511d528aa5858ff4ddf9f1a1ab45d7b1 100644 (file)
@@ -14,6 +14,8 @@
                8E9D792917BA690700C76DC9 /* SDL2_ttf.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8E9D792417BA690700C76DC9 /* SDL2_ttf.framework */; };
                8E9D792A17BA690700C76DC9 /* SDL2.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8E9D792517BA690700C76DC9 /* SDL2.framework */; };
                8EC3B7BA17BB185D00D3BDD0 /* ben.png in Copy Static Images */ = {isa = PBXBuildFile; fileRef = 8EC3B7B917BB185700D3BDD0 /* ben.png */; };
+               8EE9CAD717BBB26E00CA3A98 /* actor.c in Sources */ = {isa = PBXBuildFile; fileRef = 8EE9CAD617BBB26E00CA3A98 /* actor.c */; };
+               8EE9CADA17BBB6BF00CA3A98 /* actor_manager.c in Sources */ = {isa = PBXBuildFile; fileRef = 8EE9CAD917BBB6BF00CA3A98 /* actor_manager.c */; };
 /* End PBXBuildFile section */
 
 /* Begin PBXCopyFilesBuildPhase section */
                8E9D792417BA690700C76DC9 /* SDL2_ttf.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SDL2_ttf.framework; path = vendor/frameworks/SDL2_ttf.framework; sourceTree = "<group>"; };
                8E9D792517BA690700C76DC9 /* SDL2.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SDL2.framework; path = vendor/frameworks/SDL2.framework; sourceTree = "<group>"; };
                8EC3B7B917BB185700D3BDD0 /* ben.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = ben.png; path = data/img/static/ben.png; sourceTree = "<group>"; };
+               8EE9CAD517BBAD9000CA3A98 /* actor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = actor.h; sourceTree = "<group>"; };
+               8EE9CAD617BBB26E00CA3A98 /* actor.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = actor.c; sourceTree = "<group>"; };
+               8EE9CAD817BBB6B000CA3A98 /* actor_manager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = actor_manager.h; sourceTree = "<group>"; };
+               8EE9CAD917BBB6BF00CA3A98 /* actor_manager.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = actor_manager.c; sourceTree = "<group>"; };
 /* End PBXFileReference section */
 
 /* Begin PBXFrameworksBuildPhase section */
@@ -83,6 +89,7 @@
                8E9D78F517BA646F00C76DC9 /* src */ = {
                        isa = PBXGroup;
                        children = (
+                               8EE9CAD417BBAD6D00CA3A98 /* lib */,
                                8E9D78F617BA646F00C76DC9 /* main.c */,
                                8E9D78F717BA646F00C76DC9 /* Super_Polarity.1 */,
                        );
                        name = static;
                        sourceTree = "<group>";
                };
+               8EE9CAD417BBAD6D00CA3A98 /* lib */ = {
+                       isa = PBXGroup;
+                       children = (
+                               8EE9CAD517BBAD9000CA3A98 /* actor.h */,
+                               8EE9CAD617BBB26E00CA3A98 /* actor.c */,
+                               8EE9CAD817BBB6B000CA3A98 /* actor_manager.h */,
+                               8EE9CAD917BBB6BF00CA3A98 /* actor_manager.c */,
+                       );
+                       name = lib;
+                       sourceTree = "<group>";
+               };
 /* End PBXGroup section */
 
 /* Begin PBXNativeTarget section */
                        buildActionMask = 2147483647;
                        files = (
                                8E9D78F817BA646F00C76DC9 /* main.c in Sources */,
+                               8EE9CAD717BBB26E00CA3A98 /* actor.c in Sources */,
+                               8EE9CADA17BBB6BF00CA3A98 /* actor_manager.c in Sources */,
                        );
                        runOnlyForDeploymentPostprocessing = 0;
                };
index 11dbff62223546b197386a0bc14a202848d0d05b..05ba0739f6a8f953805c5280d21226be4614a633 100644 (file)
Binary files a/Super Polarity.xcodeproj/project.xcworkspace/xcuserdata/benbeltran.xcuserdatad/UserInterfaceState.xcuserstate and b/Super Polarity.xcodeproj/project.xcworkspace/xcuserdata/benbeltran.xcuserdatad/UserInterfaceState.xcuserstate differ
diff --git a/Super Polarity.xcodeproj/xcuserdata/benbeltran.xcuserdatad/xcdebugger/Breakpoints.xcbkptlist b/Super Polarity.xcodeproj/xcuserdata/benbeltran.xcuserdatad/xcdebugger/Breakpoints.xcbkptlist
new file mode 100644 (file)
index 0000000..a05e410
--- /dev/null
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<Bucket
+   type = "1"
+   version = "1.0">
+   <FileBreakpoints>
+      <FileBreakpoint
+         shouldBeEnabled = "Yes"
+         ignoreCount = "0"
+         continueAfterRunningActions = "No"
+         filePath = "src/actor.h"
+         timestampString = "398180339.270324"
+         startingColumnNumber = "9223372036854775807"
+         endingColumnNumber = "9223372036854775807"
+         startingLineNumber = "26"
+         endingLineNumber = "26">
+      </FileBreakpoint>
+   </FileBreakpoints>
+</Bucket>
diff --git a/src/actor.c b/src/actor.c
new file mode 100644 (file)
index 0000000..32028a1
--- /dev/null
@@ -0,0 +1,44 @@
+//
+//  actor.c
+//  Super Polarity
+//
+//  Created by Ruben Beltran del Rio on 8/14/13.
+//  Copyright (c) 2013 Abuguet. All rights reserved.
+//
+
+#include <stdio.h>
+#include "actor.h"
+
+SDL_Renderer *renderer;
+
+void actorUpdate(Actor *this, Uint32 dt) {
+    this->textureBox.x = this->pos.x;
+    this->textureBox.y = this->pos.y;
+    this->angle = (Uint32)(this->angle + dt / 5) % 360;
+}
+
+void actorDraw(Actor *this) {
+    SDL_RenderCopyEx(renderer, this->texture, NULL, &this->textureBox, this->angle, NULL, SDL_FLIP_NONE);
+}
+
+// This constructor is pretty much dumb. So, don't take it as anything final.
+Actor* createActor () {
+    Actor *actor = malloc(sizeof(Actor*));
+    actor->pos.x = 0;
+    actor->pos.y = 0;
+    actor->update = actorUpdate;
+    actor->draw = actorDraw;
+    actor->angle = 0.0;
+    
+    // Load the surface.
+    SDL_Surface *benSurface;
+    benSurface = IMG_Load("data/img/static/ben.png");
+    actor->texture = SDL_CreateTextureFromSurface(renderer, benSurface);
+    actor->textureBox.x = 230;
+    actor->textureBox.y = 150;
+    actor->textureBox.w = 180;
+    actor->textureBox.h = 180;
+    SDL_FreeSurface(benSurface);
+    
+    return actor;
+}
\ No newline at end of file
diff --git a/src/actor.h b/src/actor.h
new file mode 100644 (file)
index 0000000..8bb7a88
--- /dev/null
@@ -0,0 +1,30 @@
+//
+//  actor.h
+//  Super Polarity
+//
+//  Created by Ruben Beltran del Rio on 8/14/13.
+//  Copyright (c) 2013 Abuguet. All rights reserved.
+//
+
+#ifndef Super_Polarity_actor_h
+#define Super_Polarity_actor_h
+
+#include "SDL2/SDL.h"
+#include "SDL2_image/SDL_image.h"
+
+typedef struct actor_struct Actor;
+
+typedef struct actor_struct {
+    SDL_Point pos;
+    SDL_Point vel;
+    SDL_Point acc;
+    SDL_Rect textureBox;
+    SDL_Texture *texture;
+    double angle;
+    void (*update)(Actor *, Uint32);
+    void (*draw)(Actor *);
+} Actor;
+
+Actor* createActor();
+
+#endif
diff --git a/src/actor_manager.c b/src/actor_manager.c
new file mode 100644 (file)
index 0000000..c9303d2
--- /dev/null
@@ -0,0 +1,57 @@
+//
+//  actor_manager.c
+//  Super Polarity
+//
+//  Created by Ruben Beltran del Rio on 8/14/13.
+//  Copyright (c) 2013 Abuguet. All rights reserved.
+//
+
+#include <stdio.h>
+#include "actor_manager.h"
+
+void actorManagerUpdate(ActorManager *this, Uint32 dt) {
+    ActorNode *head = this->actors;
+    
+    while (head != NULL) {
+        head->val->update(head->val, dt);
+        head = head->next;
+    }
+}
+
+void actorManagerDraw(ActorManager *this) {
+    ActorNode *head = this->actors;
+    
+    while (head != NULL) {
+        head->val->draw(head->val);
+        head = head->next;
+    }
+}
+
+void actorManagerAddActor(ActorManager *this, Actor *actor) {
+    ActorNode *actorNode = malloc(sizeof(ActorNode*));
+    ActorNode **head = &this->actors;
+    ActorNode *temp;
+    actorNode->val = actor;
+    actorNode->next = NULL;
+    
+    if (*head == NULL) {
+        *head = actorNode;
+    } else {
+        temp = *head;
+        
+        while (temp->next != NULL) {
+            temp = temp->next;
+        }
+        temp->next = actorNode;
+    }
+}
+
+ActorManager* createActorManager () {
+    ActorManager *actorManager = malloc(sizeof(ActorManager*));
+    actorManager->actors = NULL;
+    actorManager->update = actorManagerUpdate;
+    actorManager->draw = actorManagerDraw;
+    actorManager->addActor = actorManagerAddActor;
+    
+    return actorManager;
+}
\ No newline at end of file
diff --git a/src/actor_manager.h b/src/actor_manager.h
new file mode 100644 (file)
index 0000000..900c57c
--- /dev/null
@@ -0,0 +1,32 @@
+//
+//  actor_manager.h
+//  Super Polarity
+//
+//  Created by Ruben Beltran del Rio on 8/14/13.
+//  Copyright (c) 2013 Abuguet. All rights reserved.
+//
+
+#ifndef Super_Polarity_actor_manager_h
+#define Super_Polarity_actor_manager_h
+
+#include "SDL2/SDL.h"
+#include "actor.h"
+
+typedef struct actor_manager ActorManager;
+typedef struct actor_node ActorNode;
+
+typedef struct actor_manager {
+    ActorNode *actors;
+    void (*update)(ActorManager *, Uint32);
+    void (*draw)(ActorManager *);
+    void (*addActor)(ActorManager *, Actor *);
+} ActorManager;
+
+typedef struct actor_node {
+    Actor *val;
+    ActorNode *next;
+} ActorNode;
+
+ActorManager* createActorManager();
+
+#endif
index b51a79d2b4066289518b36c00e51bc6a6f059c75..ae73a22f2177bfe4cdf70ebe81ed9cfcc9270bd7 100644 (file)
@@ -13,6 +13,9 @@
 #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
@@ -22,7 +25,9 @@ SDL_Window *window;
 SDL_Renderer *renderer;
 SDL_Texture *benHead;
 SDL_Rect benRect;
-double angle = 0;
+bool focus;
+
+ActorManager *actorManager;
 
 void init () {
     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
@@ -47,27 +52,41 @@ void close () {
     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() {
-    // Load the surface.
-    SDL_Surface *benSurface;
-    benSurface = IMG_Load("data/img/static/ben.png");
-    benHead = SDL_CreateTextureFromSurface(renderer, benSurface);
-    benRect.x = 230;
-    benRect.y = 150;
-    benRect.w = 180;
-    benRect.h = 180;
-    SDL_FreeSurface(benSurface);
+    initActorManager();
 }
 
-void render (SDL_Renderer *renderer) {
+void draw (SDL_Renderer *renderer) {
     SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
     SDL_RenderClear(renderer);
-    SDL_RenderCopyEx(renderer, benHead, NULL, &benRect, angle, NULL, SDL_FLIP_NONE);
+    
+    actorManager->draw(actorManager);
+    
     SDL_RenderPresent(renderer);
 }
 
-void update (dt) {
-    angle = (Uint32)(angle + dt / 5) % 360;
+void update (Uint32 dt) {
+    actorManager->update(actorManager, dt);
 }
 
 int main(int argc, const char * argv[])
@@ -86,6 +105,7 @@ int main(int argc, const char * argv[])
    
     
     done = 0;
+    focus = true;
     
     // Event Loop.
     while (!done) {
@@ -98,10 +118,20 @@ int main(int argc, const char * argv[])
             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;
+                }
+            }
         }
         
-        update(dt);
-        render(renderer);
+        if (focus) {
+            update(dt);
+            draw(renderer);
+        }
         
         endFrame = SDL_GetTicks();