diff options
| author | Ben Beltran <ben@freshout.us> | 2013-08-14 09:00:22 -0500 |
|---|---|---|
| committer | Ben Beltran <ben@freshout.us> | 2013-08-14 09:00:22 -0500 |
| commit | 8534e46e400268c5ceffb3b14f02cef39eedae8f (patch) | |
| tree | ec864c10f35c91c87711b0a12710ee18ba81f9dc /src | |
| parent | ed603039c4a456b37aefe434a3b1a5352362d86b (diff) | |
Add some basic actorstuffs
Diffstat (limited to 'src')
| -rw-r--r-- | src/actor.c | 44 | ||||
| -rw-r--r-- | src/actor.h | 30 | ||||
| -rw-r--r-- | src/actor_manager.c | 57 | ||||
| -rw-r--r-- | src/actor_manager.h | 32 | ||||
| -rw-r--r-- | src/main.c | 62 |
5 files changed, 209 insertions, 16 deletions
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 <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 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 <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 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 @@ -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(); |