From: Ben Beltran Date: Wed, 14 Aug 2013 14:00:22 +0000 (-0500) Subject: Add some basic actorstuffs X-Git-Url: https://git.r.bdr.sh/rbdr/super-polarity/commitdiff_plain/8534e46e400268c5ceffb3b14f02cef39eedae8f Add some basic actorstuffs --- diff --git a/Super Polarity.xcodeproj/project.pbxproj b/Super Polarity.xcodeproj/project.pbxproj index 364ccf7..b39b0e2 100644 --- a/Super Polarity.xcodeproj/project.pbxproj +++ b/Super Polarity.xcodeproj/project.pbxproj @@ -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 */ @@ -40,6 +42,10 @@ 8E9D792417BA690700C76DC9 /* SDL2_ttf.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SDL2_ttf.framework; path = vendor/frameworks/SDL2_ttf.framework; sourceTree = ""; }; 8E9D792517BA690700C76DC9 /* SDL2.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SDL2.framework; path = vendor/frameworks/SDL2.framework; sourceTree = ""; }; 8EC3B7B917BB185700D3BDD0 /* ben.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = ben.png; path = data/img/static/ben.png; sourceTree = ""; }; + 8EE9CAD517BBAD9000CA3A98 /* actor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = actor.h; sourceTree = ""; }; + 8EE9CAD617BBB26E00CA3A98 /* actor.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = actor.c; sourceTree = ""; }; + 8EE9CAD817BBB6B000CA3A98 /* actor_manager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = actor_manager.h; sourceTree = ""; }; + 8EE9CAD917BBB6BF00CA3A98 /* actor_manager.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = actor_manager.c; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -83,6 +89,7 @@ 8E9D78F517BA646F00C76DC9 /* src */ = { isa = PBXGroup; children = ( + 8EE9CAD417BBAD6D00CA3A98 /* lib */, 8E9D78F617BA646F00C76DC9 /* main.c */, 8E9D78F717BA646F00C76DC9 /* Super_Polarity.1 */, ); @@ -113,6 +120,17 @@ name = static; sourceTree = ""; }; + 8EE9CAD417BBAD6D00CA3A98 /* lib */ = { + isa = PBXGroup; + children = ( + 8EE9CAD517BBAD9000CA3A98 /* actor.h */, + 8EE9CAD617BBB26E00CA3A98 /* actor.c */, + 8EE9CAD817BBB6B000CA3A98 /* actor_manager.h */, + 8EE9CAD917BBB6BF00CA3A98 /* actor_manager.c */, + ); + name = lib; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -165,6 +183,8 @@ buildActionMask = 2147483647; files = ( 8E9D78F817BA646F00C76DC9 /* main.c in Sources */, + 8EE9CAD717BBB26E00CA3A98 /* actor.c in Sources */, + 8EE9CADA17BBB6BF00CA3A98 /* actor_manager.c in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Super Polarity.xcodeproj/project.xcworkspace/xcuserdata/benbeltran.xcuserdatad/UserInterfaceState.xcuserstate b/Super Polarity.xcodeproj/project.xcworkspace/xcuserdata/benbeltran.xcuserdatad/UserInterfaceState.xcuserstate index 11dbff6..05ba073 100644 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 index 0000000..a05e410 --- /dev/null +++ b/Super Polarity.xcodeproj/xcuserdata/benbeltran.xcuserdatad/xcdebugger/Breakpoints.xcbkptlist @@ -0,0 +1,18 @@ + + + + + + + diff --git a/src/actor.c b/src/actor.c new file mode 100644 index 0000000..32028a1 --- /dev/null +++ b/src/actor.c @@ -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 +#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 index 0000000..8bb7a88 --- /dev/null +++ b/src/actor.h @@ -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 index 0000000..c9303d2 --- /dev/null +++ b/src/actor_manager.c @@ -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 +#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 index 0000000..900c57c --- /dev/null +++ b/src/actor_manager.h @@ -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 diff --git a/src/main.c b/src/main.c index b51a79d..ae73a22 100644 --- a/src/main.c +++ b/src/main.c @@ -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();