[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: OpenGL questions



> Now, I commented it (no, no really changes) that anybody on the list can
> have a look on it... mail me if you think something's wrong/could be done
> easier/safer...
> Oh, and read the comments before compiling and testing it! :)

Sorry, forgot the attachment :)
/* I added/updated: - init_graphics();
 		    - set_palette();
  		    - close_graphics();

Well, now... as you can see I updated/added the sections above... I
*WOULDN'T* compile and run this code before looking at it! Maybe the svgalib
crashes your system... I'm sorry I haven't enough time now to
compile/test/debug this code now, I'll do it tomorrow :)

It does: Initialize the graphic mode with a... well... usual palette :)
It does NOT so far: paint/draw anything on the screen

If you've any questions about it, MAIL ME! :)
It isn't as hard as it looks like... but it is as dirty ;)

I think the save/load option is also not a big problem, I'll also do it
tomorrow...

Peter Feld
nachoman@sb.saar.de

*/
 		  
int game_over;
int program_over;
int option_new_game;


#include <vga.h>      /* I don't like it, but what else? SUGGESTIONS! :) */
#include <vgagl.h>

/*
#define SCREEN_WIDTH 640	// I think that's obsolete
#define SCREEN_HEIGHT 480	// 'cause vgagl sets the wanted variables (WIDTH, HEIGHT, depending on the VGAMODE)
*/

#define VGAMODE 1	/* Gotta find a better solution soon :) */
 
int action;
enum {TURN_RIGHT,TURN_LEFT,THRUST};

#define SHIP_NUM_DIRECTIONS 8
#define SHIP_MAX_VELOCITY 10
int SHIP_THRUST_X[SHIP_NUM_DIRECTIONS]={0,1,1,1,0,-1,-1,-1};
int SHIP_THRUST_Y[SHIP_NUM_DIRECTIONS]={-1,-1,0,1,1,1,0,-1};

/* Let's grab some screens */
GraphicsContext *physical;
GraphicsContext *bufferscreen;
GraphicsContext *nullscreen;

struct ship_t
{
     int x;
     int y;
     int vx;
     int vy;
     int direction;
} ship;

void init_graphics()
{
	vga_init();			/* Initialize the graphics mode */
	vga_setmode(VGAMODE);		/* Set the graphics mode */
	gl_setcontextvga(VGAMODE);	/* set the screen context */
	
	physical = gl_allocatecontext();	/* Grab a physical screen */
        gl_getcontext(physical);		/* Apply the physical screen */
     
        gl_setcontextvgavirtual(VGAMODE);	/* Set the mode... again :) */
        bufferscreen = gl_allocatecontext();	/* Grab a virtual buffer screen */
        gl_getcontext(bufferscreen);		/* Apply the bufferscreen */
	                                
        gl_setcontextvgavirtual(VGAMODE);	/* Set the mode... again and again */
        nullscreen = gl_allocatecontext();	/* as the name says.. a nullscreen... completely blank */
        gl_getcontext(nullscreen);		/* Well, apply it also */

	set_palette();				/* set the palette :) */
	
        gl_clearscreen(0);			/* clears the screen with color 0 (black, you guessed it ;)*/
}

void set_palette()
{
        /* Some stolen code... I'm too lazy to write that stuff new now :) */
        Palette pal;
        int i;
        for (i = 0; i < 256; i++)
        {
		int r, g, b;
     		r = g = b = 0;
      		if ((i & 32) > 0)
       			r = (i & 31) << 1;
       		if ((i & 64) > 0)
       			g = (i & 31) << 1;
      		if ((i & 128) > 0)
       			b = (i & 31) << 1;
       		if (i < 32)
                {
 	        	r = (i & 3) << 4;   /* 2 bit */
        		g = (i & 4) << 3;   /* 1 bit */
       			b = (i & 24) << 1;  /* 2 bit */
                }
        	pal.color[i].red = r;
       		pal.color[i].green = g;
        	pal.color[i].blue = b;
	}
	gl_setpalette(&pal);
}

void init_sound()
{
}

void init_network_connection()
{
}

void init_program()
{
     program_over=0;
     init_graphics();
     init_sound();
     init_network_connection();
}

void get_options()
{
     option_new_game=1;
}

void init_game()
{
     game_over=0;
     ship.x=SCREEN_WIDTH/2;
     ship.y=SCREEN_HEIGHT/2;
     ship.vx=0;
     ship.vy=0;
     ship.direction=0;
}

void get_input()
{
}

void process_input()
{
     switch (action){
     case TURN_LEFT:
	  ship.direction--;
	  ship.direction%=SHIP_NUM_DIRECTIONS;
	  break;
     case TURN_RIGHT:
	  ship.direction++;
	  ship.direction%=SHIP_NUM_DIRECTIONS;
	  break;
     case THRUST:
	  ship.vx+=SHIP_THRUST_X[ship.direction];
	  ship.vy+=SHIP_THRUST_Y[ship.direction];
	  if (ship.vx>SHIP_MAX_VELOCITY) ship.vx=SHIP_MAX_VELOCITY;
	  if (ship.vy>SHIP_MAX_VELOCITY) ship.vy=SHIP_MAX_VELOCITY;
	  if (ship.vx<-SHIP_MAX_VELOCITY) ship.vx=-SHIP_MAX_VELOCITY;
	  if (ship.vy<-SHIP_MAX_VELOCITY) ship.vy=-SHIP_MAX_VELOCITY;
	  break;
     default:
     }       
}

void update_game()
{
     game_over=1;
}

void update_graphics()
{
}

void save_game()
{
}

void exit_game()
{
     program_over=1;
     save_game();
}

void close_graphics()
{
	vga_setmode(0);

/* reset graphics mode to TEXT mode (yes, that's 0)
   oh, and it's a bad idea to uncomment this... can cause your system crashes */

}

void close_sound()
{
}

void close_network_connection()
{
}

void exit_program()
{
     close_graphics();
     close_sound();
     close_network_connection();
}

int main(int argc,char **argv)
{
     init_program();
     
     while(!program_over){
	  get_options();
	  if (option_new_game){
	       init_game();
	       do{
		    get_input();
		    process_input();
		    update_game();
		    update_graphics();
	       } while (!game_over);
	       exit_game();
	  }
     }
     
     exit_program();
     
     return 0;
}