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 */
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;
};
--- /dev/null
+<?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>
--- /dev/null
+//
+// 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
--- /dev/null
+//
+// 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
--- /dev/null
+//
+// 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
--- /dev/null
+//
+// 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
#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
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) {
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[])
done = 0;
+ focus = true;
// Event Loop.
while (!done) {
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();