[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Linroids or whatever - to get started
Here's the source, improved again (improved -- NOT stable ;)
Take a look at it and mail me your opinions or just improve it again...
btw: it's no more called simple.c, it's svgalib.c now (as Brett wanted :)
Peter
--
Peter Feld
/* I added/updated: - save_game();
- load_game();
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... And I player around with the block read/write
functions :)
It _would_ do: Save the game / Load the game
If you've any questions about it, MAIL ME! :)
It isn't as hard as it looks like... but it is as dirty ;)
Peter Feld
nachoman@sb.saar.de
*/
#include <stdio.h> /* Used for the file commands */
#include <stdlib.h> /* this too (besides, it's always a good idea to include these files :) */
#include <vga.h> /* I don't like it, but what else? SUGGESTIONS! :) */
#include <vgagl.h> /* also used for graphics */
/*
#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 :) */
#define SAVEGAME_NAME simple.sav
#define SHIP_X_OFFSET 00
#define SHIP_Y_OFFSET 04
int game_over;
int program_over;
int option_new_game;
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()
{
FILE *savegame;
savegame = fopen(SAVEGAME_NAME, "w+");
fwrite(ship.vx, sizeof(ship.vx), 1, savegame);
fwrite(ship.vy, sizeof(ship.vy), 1, savegame);
fclose(savegame);
}
void load_game()
{
int shipx, shipx;
FILE *loadgame;
if ((loadgame = fopen(SAVEGAME_NAME, "r")) == NULL)
printf("Error in load_game(): File not found");
else
{
fseek(loadfile, SHIP_X_OFFSET, SEEK_SET);
fread(shipx, sizeof(shipx), 1, loadfile); /* Don't know wether or not this works, but sounds good, doesn't it? :) */
fseek(loadfile, SHIP_Y_OFFSET, SEEK_SET);
fread(shipy, sizeof(shipy), 1, loadfile); /* see above :) */
fclose(loadgame);
}
}
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;
}