view sgoex.c @ 122:5219bf3d5e71

title_draw
author e085768
date Mon, 27 Jun 2011 19:44:46 +0900
parents 31dd5c07f7c1
children
line wrap: on
line source

/*
  スプライト管理関数
*/

//#include<libps.h>
#include <stdio.h>
#include <SDL.h>
#include "SDL_image.h"
#include "SDL_opengl.h"
#include "texture.h"
#include "object.h"
#include "tree_controll.h"
#include "sgoex.h"
#include "trace.h"
#include "syokika.h"
#include "LoadSprite.h"


#define OT_LENGTH	1	/*  オーダリングテーブル */
#define MAXOBJ    320		/*スプライト表示上限 */

#define TRUE	1
#define FALSE	0


//static u_short tpage;
//static SDL_Rect rect;

//static int nActiveBuff;		/*ばふぁ */
// int i;				/* Multi-purpose */
//static int pageno;

//static int padd;

static const int sgo_tpx[12] =
{ 0, 64, 128, 192, 256, 320, 0, 64, 128, 192, 256, 320 };
static const int sgo_tpy[12] = { 0, 0, 0, 0, 0, 0, 256, 256, 256, 256, 256, 256 };

/*  sgo.h  独自の変数形  */
SpriteTable sptable[DEFOBJ];
// static SpriteView spview[MAXOBJ];

#define IMAGE_ADJUSTMENT (0) 

/**
 * 一つの画像をpageno(page number)で区切るtexture_page_offsetで
 * pagenoに対応した領域のx,yを用意しておく。PlayStationではpageno
 * で区切る必要があったのだろうが、PS2では全く意味はなさない。
 */
static const struct texture_page_offset {
    int x;
    int y;
} texpage_offset[] = {
    {0, 0}, {128 + IMAGE_ADJUSTMENT, 0}, {256 + IMAGE_ADJUSTMENT, 0}, {384 + IMAGE_ADJUSTMENT, 0}};

/*-------------------------------------------------------------
  関数プロトタイプ
  ---------------------------------------------------------------*/

void SDL_GL_Enter2DMode()
{
    //	SDL_Surface *sc = SDL_GetVideoSurface();

    /* Note, there may be other things you need to change,
       depending on how you have your OpenGL state set up.
    */
    glPushAttrib(GL_ENABLE_BIT);
    glDisable(GL_DEPTH_TEST);
    glDisable(GL_CULL_FACE);
    glEnable(GL_TEXTURE_2D);

    /* This allows alpha blending of 2D textures with the scene */
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    glViewport(0, 0, screen->w, screen->h);

    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();

    glOrtho(0.0, (GLdouble)screen->w, (GLdouble)screen->h, 0.0, 0.0, 1.0);

    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glLoadIdentity();

    //	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
}


void SDL_GL_Leave2DMode()
{
    glMatrixMode(GL_MODELVIEW);
    glPopMatrix();

    glMatrixMode(GL_PROJECTION);
    glPopMatrix();

    glPopAttrib();
}


static int power_of_two(int input)
{
    int value = 1;

    while ( value < input ) {
	value <<= 1;
    }
    return value;
}

void
DefSpriteEx(int number, short middlex, short middley)
{
    sptable[number].mx = middlex;
    sptable[number].my = middley;
}

void
DefSprite(int number, const char *name, float w, float h, int color, OBJECT *obj)
{
    SURFACE *surfaces;
    surfaces = search_node(obj, name);
    if(surfaces == NULL)
    {
	fprintf(stderr, "can't get node\n");
	printf("%s", name);
	SDL_Quit();
	exit(1);
    }
    sptable[number].w = w;
    sptable[number].h = h;
    sptable[number].color = (color & 32);
    sptable[number].mx = w / 2;
    sptable[number].my = h / 2;
    sptable[number].tex_w = power_of_two(sptable[number].w);
    sptable[number].tex_h = power_of_two(sptable[number].h);
    texMinX[number] = 0.0f;
    texMinY[number] = 0.0f;
    texMaxX[number] = (GLfloat)sptable[number].w / sptable[number].tex_w;
    texMaxY[number] = (GLfloat)sptable[number].h / sptable[number].tex_h;  
//    printf("texMaxX = %f, w = %d, tex_w = %d\n", texMaxX[number], sptable[number].w, sptable[number].tex_w);
    sptable[number].texture = surfaces->texture;
}

void
PutSprite(int zorder, short x, short y, int number)
{
  glBindTexture(GL_TEXTURE_2D, (GLuint&)sptable[number].texture);
  glEnable(GL_BLEND);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  glBegin(GL_QUADS);
  glTexCoord2f(texMinX[number],texMinY[number]); glVertex2i((GLuint)x,   (GLuint)y  );
  glTexCoord2f(texMinX[number],texMaxY[number]); glVertex2i((GLuint)x, (GLuint)(y+sptable[number].tex_h)  );
  glTexCoord2f(texMaxX[number],texMaxY[number]); glVertex2i((GLuint)(x+sptable[number].tex_w),   (GLuint)(y+sptable[number].tex_h));
  glTexCoord2f(texMaxX[number],texMinY[number]); glVertex2i((GLuint)(x+sptable[number].tex_w), (GLuint)y);
  glEnd();
  glDisable(GL_BLEND);
}

void
PutSpriteEx(int number, int x, int y, float scalex, float scaley, float angle)
{
    SpriteTable *m = &sptable[number];
    x -= m->w;
    y -= m->h;

    SDL_GL_Enter2DMode();
    glEnable(GL_TEXTURE_2D);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glBindTexture(GL_TEXTURE_2D, (GLuint&)sptable[number].texture);
    glTranslatef(x + m->w/2, y + m->h/2, 0.0);
    glRotatef(angle, 0.0, 0.0, 1.0);
    glScalef(scalex, scaley, 1.0);
    glBegin(GL_TRIANGLE_STRIP);
    {
	glTexCoord2f(texMinX[number], texMinY[number]); glVertex2i(-m->w/2, -m->h/2);
	glTexCoord2f(texMaxX[number], texMinY[number]); glVertex2i( m->w/2, -m->h/2);
	glTexCoord2f(texMinX[number], texMaxY[number]); glVertex2i(-m->w/2,  m->h/2);
	glTexCoord2f(texMaxX[number], texMaxY[number]); glVertex2i( m->w/2,  m->h/2);
    }
    glEnd();
    glDisable(GL_TEXTURE_2D);
    glDisable(GL_BLEND);
    SDL_GL_Leave2DMode();
    //  SDL_GL_SwapBuffers();
}



struct SGO_PAD pad[2];

#ifdef ENABLE_TRACEMODE
extern int runmode;
#endif				//ENABLE_TRACEMODE

/* コントローラ状態の読み込み */
void Pad(SDL_Joystick *joy)
{
    Sint16 axis;

    SDL_JoystickUpdate();
  
    if(SDL_JoystickGetButton(joy,PS2_CROSS)==SDL_PRESSED)
	pad[0].k0++;
    else
	pad[0].k0=0;
  
    if(SDL_JoystickGetButton(joy,PS2_CIRCLE)==SDL_PRESSED)
	pad[0].k1++;
    else
	pad[0].k1=0;
  
    if(SDL_JoystickGetButton(joy,PS2_SQUARE)==SDL_PRESSED)
	pad[0].k3++;
    else
	pad[0].k3=0;
  
    if(SDL_JoystickGetButton(joy,PS2_TRIANGLE)==SDL_PRESSED)
	pad[0].k4++;
    else
	pad[0].k4=0;
  
    if(SDL_JoystickGetButton(joy,PS2_L1)==SDL_PRESSED)
	pad[0].l1++;
    else
	pad[0].l1=0;
  
    if(SDL_JoystickGetButton(joy,PS2_R1)==SDL_PRESSED)
	pad[0].r1++;
    else
	pad[0].r1=0;
  
    if(SDL_JoystickGetButton(joy,PS2_L2)==SDL_PRESSED)
	pad[0].l2++;
    else
	pad[0].l2=0;
  
    if(SDL_JoystickGetButton(joy,PS2_R2)==SDL_PRESSED)
	pad[0].r2++;
    else
	pad[0].r2=0;
  
    if(SDL_JoystickGetButton(joy,PS2_START)==SDL_PRESSED)
	pad[0].st++;
    else
	pad[0].st=0;
  
    if(SDL_JoystickGetButton(joy,PS2_SELECT)==SDL_PRESSED)
	pad[0].se++;
    else
	pad[0].se=0;
  
    if(SDL_JoystickGetButton(joy,PS2_L3)==SDL_PRESSED)
	pad[0].l3++;
    else
	pad[0].l3=0;
  
    if(SDL_JoystickGetButton(joy,PS2_R3)==SDL_PRESSED)
	pad[0].r3++;
    else
	pad[0].r3=0;
    //x
    axis=SDL_JoystickGetAxis(joy,0);
    if(axis>=3200){
	pad[0].left=0;
	pad[0].right++;
    }
    else if(axis<=-3200){
	pad[0].right=0;
	pad[0].left++;
    }
    else {
	pad[0].right=0;
	pad[0].left=0;
    }
    //y
    axis=SDL_JoystickGetAxis(joy,1);
    if(axis>=3200){
	pad[0].up=0;
	pad[0].down++;
    }
    else if(axis<=-3200){
	pad[0].down=0;
	pad[0].up++;
    }
    else {
	pad[0].down=0;
	pad[0].up=0;
    }

    if ((pad[0].l1 != 0) && (pad[0].r1 != 0) &&
	(pad[0].l2 != 0) && (pad[0].r2 != 0) &&
	(pad[0].st != 0) && (pad[0].se != 0)) {
	pad[0].quit = 1;
    } else {
	pad[0].quit = 0;
    }
	
}


void keybord()
{
    SDL_PumpEvents();
    Uint8 *keys = SDL_GetKeyState(NULL);

    if (keys[SDLK_UP]) {
	pad[0].up++;
    } else {
	pad[0].up = 0;
    }
    if (keys[SDLK_DOWN]) {
	pad[0].down++;
    } else {
	pad[0].down = 0;
    }

    if (keys[SDLK_RIGHT]) {
	pad[0].right++;
    } else {
	pad[0].right = 0;
    }

    if (keys[SDLK_LEFT]) {
	pad[0].left++;
    } else {
	pad[0].left = 0;
    }

    if (keys[SDLK_a]) {
	pad[0].k0++;
    } else {
	pad[0].k0 = 0;
    }

    if (keys[SDLK_z]) {
	pad[0].k1++;
    } else {
	pad[0].k1 = 0;
    }

    if (keys[SDLK_s]) {
	pad[0].k3++;
    } else {
	pad[0].k3 = 0;
    }
    
    if (keys[SDLK_x]) {
	pad[0].k4++;
    } else {
	pad[0].k4 = 0;
    }

    if (keys[SDLK_r]) {
	pad[0].r2++;
    } else {
	pad[0].r2 = 0;
    }

    if (keys[SDLK_e]) {
	pad[0].r1++;
    } else {
	pad[0].r1 = 0;
    }

    if (keys[SDLK_w]) {
	pad[0].l1++;
    } else {
	pad[0].l1 = 0;
    }

    if (keys[SDLK_q]) {
	pad[0].l2++;
    } else {
	pad[0].l2 = 0;
    }

    // START ボタンは Return が似合う気がする
    //if(keys[SDLK_1])
    if (keys[SDLK_RETURN]) {
	pad[0].st++;
    } else {
	pad[0].st = 0;
    }

    if (keys[SDLK_2]) {
	pad[0].se++;
    } else {
	pad[0].se = 0;
    }

    if (keys[SDLK_ESCAPE]) {
	SDL_Quit();
	exit(1);
	//pad[0].st = 1;
	//pad[0].se = 1;
    }

    if (keys[SDLK_0]) {
	pad[0].quit = 1;
    } else {
	pad[0].quit = 0;
    }
}