SDLGameEngine

- Main Page
- Related Pages
- Modules
- Data Structures
- Files

- File List
- Globals

# src/sgestage.c

    00001 /*
    00002  * Copyright (c) 2007 Heiko Irrgang
    00003  *
    00004  * The license and distribution terms for this file may be
    00005  * found in the file COPYING in this distribution or at
    00006  * http://93-interactive.com/cms/products/software/sdl-game-engine/license/
    00007  */
    00008
    00009 #include <sge.h>
    00010
    00011 SGELAYER *sgeLayerNew(SGESPRITE *sprite) {
    00012         SGELAYER *ret;
    00013         SGESPRITEIMAGE *i=sgeArrayGet(sgeSpriteGetCurrentSpriteArray(sprite), sprite->currentFrame);
    00014         sgeNew(ret, SGELAYER);
    00015         ret->x=0;
    00016         ret->y=0;
    00017         ret->w=i->image->w;
    00018         ret->h=i->image->h;
    00019         ret->sprite=sprite;
    00020         return ret;
    00021 }
    00022
    00023 void sgeLayerDestroy(SGELAYER *l) {
    00024         sgeSpriteDestroy(l->sprite);
    00025         sgeFree(l);
    00026 }
    00027
    00028 SGESTAGE *sgeStageNew(int width, int height) {
    00029         SGESTAGE *ret;
    00030         sgeNew(ret, SGESTAGE);
    00031         ret->cameraX=0;
    00032         ret->cameraY=0;
    00033         ret->w=width;
    00034         ret->h=height;
    00035         ret->layers=sgeArrayNew();
    00036         ret->spriteGroups=sgeArrayNew();
    00037         return ret;
    00038 }
    00039
    00040 static void sgeStageDestroyLayersHelper(Uint32 id, void *data) {
    00041         SGELAYER *l=(SGELAYER *)data;
    00042         sgeLayerDestroy(l);
    00043 }
    00044
    00045 static void sgeStageDestroySpriteGroupsHelper(Uint32 id, void *data) {
    00046         SGESPRITEGROUP *g=(SGESPRITEGROUP *)data;
    00047         sgeSpriteGroupDestroy(g);
    00048 }
    00049
    00050 void sgeStageDestroy(SGESTAGE *s) {
    00051         sgeArrayForEach(s->layers, sgeStageDestroyLayersHelper);
    00052         sgeArrayDestroy(s->layers);
    00053         sgeArrayForEach(s->spriteGroups, sgeStageDestroySpriteGroupsHelper);
    00054         sgeArrayDestroy(s->spriteGroups);
    00055         sgeFree(s);
    00056 }
    00057
    00058 int sgeStageAddLayer(SGESTAGE *s, SGESPRITE *sprite, int x, int y) {
    00059         SGELAYER *l;
    00060         l=sgeLayerNew(sprite);
    00061         l->x=x;
    00062         l->y=y;
    00063         sgeArrayAdd(s->layers, l);
    00064         return s->layers->numberOfElements-1;
    00065 }
    00066
    00067 void sgeStageSetLayerHeight(SGESTAGE *s, int layer, int height) {
    00068         SGELAYER *l=(SGELAYER *)sgeArrayGet(s->layers, layer);
    00069         l->h=height;
    00070 }
    00071
    00072 void sgeStageSetLayerWidth(SGESTAGE *s, int layer, int width) {
    00073         SGELAYER *l=(SGELAYER *)sgeArrayGet(s->layers, layer);
    00074         l->w=width;
    00075 }
    00076
    00077 void sgeStageDrawLayer(SGESTAGE *s, SDL_Surface *dest, int layer) {
    00078         SDL_Rect r;
    00079         SGELAYER *l=(SGELAYER *)sgeArrayGet(s->layers, layer);
    00080         SGEARRAY *cur=sgeSpriteGetCurrentSpriteArray(l->sprite);
    00081         SGESPRITEIMAGE *i=sgeArrayGet(cur, l->sprite->currentFrame);
    00082         int newx, newy;
    00083         double tmp;
    00084         newx=s->cameraX-l->x;
    00085         if (l->x!=0) {
    00086                 newx-=screen->w;
    00087         }
    00088         newy=s->cameraY-l->y;
    00089         if (l->y!=0) {
    00090                 newy-=screen->h;
    00091         }
    00092         tmp=(double)newx*(double)((double)(i->w+l->x-screen->w)/(double)s->w);
    00093         r.x=(int)tmp;
    00094         tmp=(double)newy*(double)((double)(i->h+l->y-screen->h)/(double)s->h);
    00095         r.y=(int)tmp;
    00096         r.w=dest->w;
    00097         r.h=dest->h;
    00098
    00099         SDL_BlitSurface(i->image, &r, dest, NULL);
    00100 }
    00101
    00102 int sgeStageAddSpriteGroup(SGESTAGE *s, SGESPRITEGROUP *g) {
    00103         sgeArrayAdd(s->spriteGroups, g);
    00104         return s->spriteGroups->numberOfElements-1;
    00105 }
    00106
    00107 int sgeStageAddSprite(SGESTAGE *s, int spriteGroup, SGESPRITE *sprite) {
    00108         SGESPRITEGROUP *g=sgeArrayGet(s->spriteGroups,spriteGroup);
    00109         sgeSpriteGroupAddSprite(g, sprite);
    00110         return g->sprite->numberOfElements-1;
    00111 }
    00112
    00113 void sgeStageDrawSpriteGroup(SGESTAGE *s, int spriteGroup) {
    00114         SGESPRITEGROUP *g=sgeArrayGet(s->spriteGroups,spriteGroup);
    00115         sgeSpriteGroupDrawRelative(g, s->cameraX, s->cameraY);
    00116 }
    00117
    00118 void sgeStageDrawSpriteGroups(SGESTAGE *s) {
    00119         Uint32 i;
    00120         SGESPRITEGROUP *g;
    00121         for (i=0;i<s->spriteGroups->numberOfElements;i++) {
    00122                 g=(SGESPRITEGROUP *)sgeArrayGet(s->spriteGroups,i);
    00123                 sgeSpriteGroupDrawRelative(g, s->cameraX, s->cameraY);
    00124         }
    00125 }
    00126
    00127 int sgeStageSpriteGroupCollideSprite(SGESTAGE *s, int b, SGESPRITE *a, int orientation) {
    00128         SGESPRITEGROUP *g;
    00129         SGESPRITE *collider;
    00130         SGESPRITEIMAGE *collidersprite, *colliderimage;
    00131         int ret=0;
    00132
    00133         if (b > s->spriteGroups->numberOfElements-1) {
    00134                 return 0;
    00135         }
    00136
    00137         g=(SGESPRITEGROUP *)sgeArrayGet(s->spriteGroups, b);
    00138         collidersprite=sgeSpriteGetCurrentFrame(a);
    00139         colliderimage=sgeSpriteImageDuplicate(collidersprite);
    00140
    00141         if (orientation==RELATIVE) {
    00142                 colliderimage->x+=s->cameraX;
    00143                 colliderimage->y+=s->cameraY;
    00144         }
    00145
    00146         collider=sgeSpriteNew();
    00147         sgeSpriteAddSpriteImage(collider, colliderimage);
    00148
    00149         ret=sgeSpriteGroupCollideSprite(g, collider);
    00150         sgeSpriteDestroy(collider);
    00151         return ret;
    00152 }
    00153
    00154 int sgeStageSpriteGroupCollideSpriteGroup(SGESTAGE *s, int a, int b, int orientationa) {
    00155         SGESPRITE *tmpa;
    00156         SGESPRITEGROUP *g;
    00157         int i;
    00158
    00159         if (
    00160                         (b > s->spriteGroups->numberOfElements-1) ||
    00161                         (a > s->spriteGroups->numberOfElements-1)
    00162         ) {
    00163                 return 0;
    00164         }
    00165
    00166         g=(SGESPRITEGROUP *)sgeArrayGet(s->spriteGroups, a);
    00167         for (i=0;i<g->sprite->numberOfElements;i++) {
    00168                 tmpa=(SGESPRITE *)sgeArrayGet(g->sprite,i);
    00169                 if (sgeStageSpriteGroupCollideSprite(s, b, tmpa, orientationa)) return 1;
    00170         }
    00171         return 0;
    00172 }
    00173
    00174 SGEPOSITION *sgeStageScreenToReal(SGESTAGE *s, int x, int y) {
    00175         return sgePositionNew(x+s->cameraX,y+s->cameraY);
    00176 }

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines