All pastes #985420 Raw Edit

Something

public text v1 · immutable
·published 2008-04-15 00:20 UTC
rendered paste body
/*
 * This Program will allow for universal editing of 
 * bzflag maps.
 *
 * 
 *
 */

/*
 *     gtkbzedit - the gtk+ bzflag editer client
 *
 *
 *  Copyright (C) 2008 Nathaniel Nelson
 *
 *  gtkatlantic is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#define _GNU_SOURCE

#include "global.h"
#include "main.h"
#include "callback.h"
#include "mesh3d.h"

int main(int argc, char **argv) {

 

  gtk_init(&argc, &argv);
  gdk_init(&argc, &argv);

g = malloc(sizeof(_global));
 d3d = malloc(sizeof(_3d_data));
   
  // get the first char

 g->t = unit_rt(); 

  if(!argc >= 2) {
    
    printf("Usage: \n gtkbzedit filename.bzw \n");
    exit(0);
  }

  g->fname = argv[1];
  g->stage = 0;
  g->made_gc =0;
  g->t_color = TRUE;
  g->m_color = FALSE;
  g->k_flip = FALSE;

  printf("g->fname = %s\n", g->fname);

  create_window();

  gtk_timeout_add(200, client_loop, NULL);

   gtk_main();

  

}

void print_object(int obj_num) {

  printf("Object Added; Number %d\n", obj_num);
  printf("Object Info:\n");
  if(o[obj_num]->name) printf("Name: %s\n", o[obj_num]->name);
  if(o[obj_num]->obj == ARC) printf("Type: ARC\n");
  if(o[obj_num]->obj == BASE) printf("Type: BASE\n");
  if(o[obj_num]->obj == BOX) printf("Type: BOX\n");
  if(o[obj_num]->obj == COLOR) printf("Type: COLOR\n");
  if(o[obj_num]->obj == CONE) printf("Type: CONE\n");
  if(o[obj_num]->obj == DEFINE) printf("Type: DEFINE\n");
  if(o[obj_num]->obj == DYNAMICCOLOR) printf("Type: DYNAMICCOLOR\n");
  if(o[obj_num]->obj == GROUP) printf("Type: GROUP\n");
  if(o[obj_num]->obj == LINK) printf("Type: LINK\n");
  if(o[obj_num]->obj == MATERIAL) printf("Type: MATERIAL\n");
  if(o[obj_num]->obj == MESH) printf("Type: MESH\n");
  if(o[obj_num]->obj == MESHBOX) printf("Type: MESHBOX\n");
  if(o[obj_num]->obj == MESHPYR) printf("Type: MESHPYR\n");
if(o[obj_num]->obj == OPTIONS) printf("Type: OPTIONS\n");
if(o[obj_num]->obj == PHYSICS) printf("Type: PHYSICS\n");
if(o[obj_num]->obj == PYRAMID) printf("Type: PYRIMID\n");
if(o[obj_num]->obj == SPHERE) printf("Type: SPHERE\n");
if(o[obj_num]->obj == TELEPORTER) printf("Type: TELEPORTER\n");
if(o[obj_num]->obj == TEXTUREMATRIX) printf("Type: TEXTUREMATRIX\n");
if(o[obj_num]->obj == WATERLEVEL) printf("Type: WATERLEVEL\n");
if(o[obj_num]->obj == WEAPON) printf("Type: WEAPON\n");
if(o[obj_num]->obj == WORLD) printf("Type: WORLD\n");
if(o[obj_num]->obj == ZONE) printf("Type: ZONE\n");
printf("Size: %f %f %f\n", o[obj_num]->size.x, o[obj_num]->size.y, o[obj_num]->size.z);
printf("Position: %f %f %f\n", o[obj_num]->pos.x, o[obj_num]->pos.y, o[obj_num]->pos.z);
printf("Rotation: %f\n\n", o[obj_num]->rotation);

 printf("Spin %f %f %f %f\n", o[obj_num]->spin.angle, o[obj_num]->spin.x, o[obj_num]->spin.y, o[obj_num]->spin.z);
printf("Shift: %f %f %f\n", o[obj_num]->shift.x, o[obj_num]->shift.y, o[obj_num]->shift.z);
printf("Shear: %f %f %f\n", o[obj_num]->shear.x, o[obj_num]->shear.y, o[obj_num]->shear.z);
printf("Scale: %f %f %f\n", o[obj_num]->scale.x, o[obj_num]->scale.y, o[obj_num]->scale.z);
 printf("Texturesize: %f %f %f %f\n", o[obj_num]->texsize[1], o[obj_num]->texsize[2], o[obj_num]->texsize[3], o[obj_num]->texsize[4]);
 if(o[obj_num]->phydrv) printf("Phydrv: %s\n", o[obj_num]->phydrv);
 printf("Divisions: %d\n", o[obj_num]->divisions);
 printf("Ratio: %d\n", o[obj_num]->ratio);
 printf("Flatshading: %d\n", o[obj_num]->flatshading);
 printf("Smoothbounce: %d\n", o[obj_num]->smoothbounce);
printf("Tcolor: %d\n", o[obj_num]->tcolor);
printf("Oncap flag id: %d\n", o[obj_num]->oncap);
printf("Tint: %f %f %f %f\n", o[obj_num]->tint[1], o[obj_num]->tint[2], o[obj_num]->tint[3], o[obj_num]->tint[4]);
 printf("Drive Through: %d\n", o[obj_num]->drivethrough);
 printf("Smoot Through: %d\n", o[obj_num]->shootthrough);
 printf("From: %s\n", o[obj_num]->from);
 printf("To: %s\n", o[obj_num]->to);
printf("border: %f\n", o[obj_num]->border);
printf("Color: %f %f %f %f\n", o[obj_num]->color[1], o[obj_num]->color[2], o[obj_num]->color[3], o[obj_num]->color[4]);

// TO DO: add matref
 
}

void new_map_object() {

o[g->n_obj] = malloc(sizeof(objects));

// o[g->n_obj]->name = NULL;
 o[g->n_obj]->obj = BOX; 

 o[g->n_obj]->size = (point){ 0.0, 0.0, 0.0};
o[g->n_obj]->pos = (point){ 0.0, 0.0, 0.0};
 o[g->n_obj]->rotation = 0.0;

 o[g->n_obj]->spin = (apoint){ 0.0, 0.0, 0.0, 0.0};
o[g->n_obj]->shift = (point){ 0.0, 0.0, 0.0};
o[g->n_obj]->shear = (point){ 0.0, 0.0, 0.0};
o[g->n_obj]->scale = (point){ 0.0, 0.0, 0.0};
//o[g->n_obj]->texsize = {0.0, 0.0, 0.0, 0.0};
 // o[g->n_obj]->phydrv = NULL;

 o[g->n_obj]->divisions =0;
 o[g->n_obj]->ratio =0;
 o[g->n_obj]->flatshading =0;
 o[g->n_obj]->smoothbounce =0;
 o[g->n_obj]->tcolor = 1;
 o[g->n_obj]->oncap = f_SW;

 //o[g->n_obj]->tint = { 0.0, 0.0, 0.0, 0.0};
 o[g->n_obj]->drivethrough = 0;
 o[g->n_obj]->shootthrough = 0;

 // o[g->n_obj]->from = NULL;
 // o[g->n_obj]->to = NULL;

 o[g->n_obj]->border = 0.0;

 // o[g->n_obj]->color = { 0.0, 0.0, 0.0, 0.0};

 o[g->n_obj]->b_color = 0;

 // o[g->n_obj]->matrefs = { 0, 0, 0, 0, 0, 0, 0};
}

gboolean client_loop() {

 FILE *map;

  if(g->stage == 0) {
 
g->stage = 1;

  map = fopen(g->fname, "r");

  if(map == NULL){
    printf("failed to open file %s\n", g->fname);
    exit(0);
  }
  
  

  g->n_obj = 0;
  g->n_box = 0;
  g->n_pyr = 0;
  g->n_base = 0;
  g->n_tel = 0;
  g->n_link = 0;

  
  g->c = fgetc(map);

  g->string = malloc(300);

  while (!(feof(map))) {
    new_map_object();
    read_map(map);
    //print_object(g->n_obj);
    g->n_obj++;
    //sleep(1);
  }
  printf("Done Reading file\n");

  printf("Read: \n");
  printf("%i objects\n", g->n_obj);
  printf("%i boxes\n", g->n_box);
  printf("%i pyramids\n", g->n_pyr);
  printf("%i bases\n", g->n_base);
  printf("%i teleports\n", g->n_tel);
  printf("%i links\n", g->n_link);
  

  fclose(map);

  // this is so that the map is not drawn to early
  g->stage = 2;
  }

  
  return(TRUE);
}

void create_window(){

  g->mainwin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title(GTK_WINDOW(g->mainwin), "Bzedit2");
  gtk_window_set_policy(GTK_WINDOW(g->mainwin), FALSE, TRUE, TRUE);
  gtk_widget_set_usize(g->mainwin, 700, 500);

  gtk_widget_set_events(g->mainwin, GDK_KEY_PRESS_MASK);

  //call backs

  gtk_signal_connect(GTK_OBJECT(g->mainwin), "delete-event", (GtkSignalFunc) cb_exit, NULL);
  gtk_signal_connect(GTK_OBJECT(g->mainwin), "destroy", (GtkSignalFunc) gtk_exit, NULL);
  gtk_signal_connect(GTK_OBJECT(g->mainwin), "key-press-event", (GtkSignalFunc) cb_keypress, NULL);

  g->w_height = 500;
  g->w_with = 700;

  g->mainbox = gtk_vbox_new(FALSE, 0);
  gtk_container_add(GTK_CONTAINER(g->mainwin), g->mainbox);

  g->draw = gtk_drawing_area_new();
  gtk_drawing_area_size(GTK_DRAWING_AREA(g->draw), 700, 500);
  gtk_box_pack_start(GTK_BOX(g->mainbox), g->draw, TRUE, TRUE, 0);

  gtk_signal_connect(GTK_OBJECT(g->draw), "expose_event", (GtkSignalFunc) cb_expose, NULL);

  g->d_with = 700;
  g->d_height = 500;

  //margian = 2

  //view port
  d3d->left = 0;
  d3d->top = 0;
  d3d->right = g->d_with;
  d3d->bottom = g->d_height;
  
  // originally all the fallowing were /2

  d3d->Ox = (d3d->right + d3d->left)/2;
  d3d->Oy = (d3d->bottom + d3d->top)/2;

  d3d->x1 = -(d3d->right + d3d->left);
  d3d->x2 = (d3d->bottom + d3d->top);
  d3d->y1 = (d3d->right + d3d->left);
  d3d->y2 = -(d3d->bottom + d3d->top);

  d3d->perspective = FALSE;
  d3d->subjective = FALSE;

  gtk_widget_show_all(g->mainwin);
}

void draw_map() {

  int i;

  // clear drawing

  gdk_draw_rectangle(g->draw->window, g->draw->style->bg_gc[GTK_WIDGET_STATE (g->draw)], TRUE, 0, 0, g->draw->allocation.width, g->draw->allocation.height);
  
  d3d->e = (point){0.0, 0.0, 800};
  draw_wall(800, g->t);

  // if done loading ....

  if(g->stage >= 2) {

  //loop and draw all objects

  for(i=0; i<g->n_obj; i++){

    if(o[i]->obj == BOX || o[i]->obj == BASE || o[i]->obj == TELEPORTER){
      draw_box(o[i]->size.x, o[i]->size.y, o[i]->size.z, o[i]->pos, o[i]->rotation, i, g->t);
    }
    else if(o[i]->obj == PYRAMID){
      draw_pyr(o[i]->size, o[i]->pos, o[i]->rotation, g->t);
    }
    else {
      //printf("not drawn yet\n");
    }

    }
  }

}

void new_frame() {

  // free old one 

  if(g->bdraw) g_free(g->bdraw);

// now make a pix map to draw on

g->bdraw = gdk_pixmap_new( g->draw->window, g->d_with, g->d_height, -1);

}

char fgetch(FILE *in) {
  char c;
  c = fgetc(in);
  
  if(feof(in)) {
    g->s = sym_eof;
    return(EOF);
  }

  return(c);

}

int phrase(FILE *in) {

  int clear;
  char *str;
  int len;
  //char c;
  bool done;
    
  len = 0;
  done = 0;

  // 9 = TAB
  // 10 = LF (line Feed)
  // 13 = CR (carage Return)

  //if(g->s == sym_eof) return(0);
  
  // clear out string
  for(clear=0; clear<200; clear++) {

     g->string[clear] = ' ';

  }

  // if(feof(in)) return(0);
  if(g->c == EOF) return(0);
  //if(g->c == '@' ) return;

  while(g->c == ' ' || g->c == '\x09' || g->c == '\n' || g->c == '\r' || g->c == '#') {
    // to do  FIX ME keep comments
    //printf("in first while first char :%c\n", g->c);
    if(g->c == '#'){
      while(1) 
	{
	  g->c = fgetc(in);
	  if(g->c == EOF) return(0);
	  putchar(g->c);
	  if(g->c == '\n' || g->c == '\r' || g->c == 0) break;
	  done = 1;
	  
	}
    }
    else g->c = fgetc(in);
    if(g->c == EOF) return(0);
    putchar(g->c);
    
    //
      
  }

  

  // printf("Secound part \n\n");

  // now see if it maches cryteria

  if(isalpha(g->c) || g->c == '_') {
    // set the string to it
    g->string[0] = g->c;
    len = 0;

    while(isalpha(g->c) || g->c == '_') {
      
      g->c = fgetc(in);
      
      if(g->c == EOF) return(0);
      g->string[len + 1] = g->c;

      putchar(g->c);
      
      

      len ++;

      //printf("%s\n", g->string);

    }
    str = strndup( g->string, len);

    if(!strncmp(str, "box", len)) g->s = sym_box; 
    else if(!strncmp(str, "position", len)) g->s = sym_position;
    else if(!strncmp(str, "rotation", len)) g->s = sym_rotation;
    else if(!strncmp(str, "size", len)) g->s = sym_size;
    else if(!strncmp(str, "end", len)) g->s = sym_end;
else if(!strncmp(str, "border", len)) g->s = sym_border;
else if(!strncmp(str, "angle", len)) g->s = sym_angle;
else if(!strncmp(str, "base", len)) g->s = sym_base;
else if(!strncmp(str, "color", len)) g->s = sym_color;
else if(!strncmp(str, "delay", len)) g->s = sym_delay;
else if(!strncmp(str, "divisions", len)) g->s = sym_divisions;
else if(!strncmp(str, "drivethrough", len)) g->s = sym_drivethrough;
else if(!strncmp(str, "flagheight", len)) g->s = sym_flagheight;
else if(!strncmp(str, "flatshading", len)) g->s = sym_flatshading;
else if(!strncmp(str, "from", len)) g->s = sym_from;
else if(!strncmp(str, "initdelay", len)) g->s = sym_initdelay;
else if(!strncmp(str, "link", len)) g->s = sym_link;
else if(!strncmp(str, "name", len)) g->s = sym_name;
else if(!strncmp(str, "passable", len)) g->s = sym_passable;
else if(!strncmp(str, "pyramid", len)) g->s = sym_pyramid;
else if(!strncmp(str, "ratio", len)) g->s = sym_ratio;
else if(!strncmp(str, "scale", len)) g->s = sym_scale;
else if(!strncmp(str, "shear", len)) g->s = sym_shear;
else if(!strncmp(str, "shift", len)) g->s = sym_shift;
else if(!strncmp(str, "shootthrough", len)) g->s = sym_shootthrough;
else if(!strncmp(str, "smoothbounce", len)) g->s = sym_smoothbounce;
else if(!strncmp(str, "teleporter", len)) g->s = sym_teleporter;
else if(!strncmp(str, "to", len)) g->s = sym_to;
else if(!strncmp(str, "type", len)) g->s = sym_type;
else if(!strncmp(str, "weapon", len)) g->s = sym_weapon;
else if(!strncmp(str, "world", len)) g->s = sym_world;
else if(!strncmp(str, "spin", len)) g->s = sym_spin;
else if(!strncmp(str, "meshbox", len)) g->s = sym_meshbox;
else if(!strncmp(str, "meshpyr", len)) g->s = sym_meshpyr;
else if(!strncmp(str, "material", len)) g->s = sym_material;
else if(!strncmp(str, "physics", len)) g->s = sym_physics;
else if(!strncmp(str, "zone", len)) g->s = sym_zone;
else if(!strncmp(str, "phydrv", len)) g->s = sym_phydrv;
else if(!strncmp(str, "matref", len)) g->s = sym_matref;
else if(!strncmp(str, "linear", len)) g->s = sym_linear;
else if(!strncmp(str, "angular", len)) g->s = sym_angular;
else if(!strncmp(str, "slide", len)) g->s = sym_slide;
else if(!strncmp(str, "death", len)) g->s = sym_death;
else if(!strncmp(str, "texture", len)) g->s = sym_texture;
else if(!strncmp(str, "addtexture", len)) g->s = sym_addtexture;
else if(!strncmp(str, "notexture", len)) g->s = sym_notexture;
else if(!strncmp(str, "notexcolor", len)) g->s = sym_notexcolor;
else if(!strncmp(str, "notexalpha", len)) g->s = sym_notexalpha;
else if(!strncmp(str, "texmat", len)) g->s = sym_texmat;
else if(!strncmp(str, "dyncol", len)) g->s = sym_dyncol;
else if(!strncmp(str, "ambient", len)) g->s = sym_ambient;
else if(!strncmp(str, "diffuse", len)) g->s = sym_diffuse;
else if(!strncmp(str, "specular", len)) g->s = sym_specular;
else if(!strncmp(str, "emission", len)) g->s = sym_emission;
else if(!strncmp(str, "shininess", len)) g->s = sym_shininess;
else if(!strncmp(str, "zoneflag", len)) g->s = sym_zoneflag;
else if(!strncmp(str, "flag", len)) g->s = sym_flag;
else if(!strncmp(str, "team", len)) g->s = sym_team;
else if(!strncmp(str, "safety", len)) g->s = sym_safety;
else if(!strncmp(str, "texturematrix", len)) g->s = sym_texturematrix;
else if(!strncmp(str, "center", len)) g->s = sym_center;
else if(!strncmp(str, "fixedscale", len)) g->s = sym_fixedscale;
else if(!strncmp(str, "fixedspin", len)) g->s = sym_fixedspin;
else if(!strncmp(str, "fixedshift", len)) g->s = sym_fixedshift;
else if(!strncmp(str, "cone", len)) g->s = sym_cone;
else if(!strncmp(str, "sphere", len)) g->s = sym_sphere;
else if(!strncmp(str, "radius", len)) g->s = sym_radius;
    


    printf("symble Number %i\n", g->s);

  }

  // printf("third part\n");

  if(isdigit(g->c) || g->c == '.' || g->c =='-') {
    // set the string to it
    g->string[0] = g->c;
    len = 0;
    g->inf = 0.0;
    

    while(isdigit(g->c) || g->c == '.' || g->c =='-') {
      
      g->c = fgetc(in);
      if(g->c == EOF) return(0);
            
      g->string[len + 1] = g->c;

      putchar(g->c);
      

      len ++;

      //printf("%s\n", g->string);



    }

    str = strndup(g->string, len);

    //str[len + 1] = '\0';
    
    //printf("%i,%s\n\n", len, str);

    //g->isint = 0;
    
    if(g->isint == TRUE) {
      g->ini = atoi(str);
     }
    
    g->inf = atof(str);
    //printf("got float: %f \n", g->inf);

    //g->s = sym_number;

  }
  //printf("forth part\n");

  


  //if(feof(in)) return(0);
  //printf("\n\nstring: %s\n\n", str);

  //g->string = strdup(str);

//sleep(1);

  //free(g->string);

 
 return (1);

}

float read_number(FILE *in) {

  float tmp;
  //if(!g->s == sym_number){
  //  printf("ERROR expected NUMBER\n");
  //  return(0.0);
  // }

  tmp = g->inf;

  //printf("\n%f\n",tmp);

  phrase(in);

  

  return(tmp);

}

void read_map(FILE *in){

  // to start malloc a block

g->isint = FALSE;

  phrase(in);

 if(feof(in)) return; 
  
 

  if(g->s == sym_box) {
    g->n_box++;
    o[g->n_obj]->obj = BOX;
  }
  if(g->s == sym_pyramid) {
    g->n_pyr++;
    o[g->n_obj]->obj = PYRAMID;
  }
  if(g->s == sym_base) {
    g->n_base++;
    o[g->n_obj]->obj = BASE;
  }
  if(g->s == sym_teleporter) {
    g->n_tel++;
    o[g->n_obj]->obj = TELEPORTER;
  }
  if(g->s == sym_link) {
    g->n_link++;
    o[g->n_obj]->obj = LINK;
  }
  
  
  

  phrase(in);

  // untill the function returns
  while(1) {

    g->isint = FALSE;


if(feof(in)) return; 

//sleep(1);

  // read symbles
  if(g->s == sym_end) {
    //phrase(in);
    return;
  }
  else if(g->s == sym_name) {
    phrase(in);
    // o[g->n_obj]->name = strdup( g->string );
    //printf("object name: %s", o[g->n_obj]->name);
     phrase(in);
  }
  else if(g->s == sym_position) {
    phrase(in);
    o[g->n_obj]->pos.x = read_number(in);
    o[g->n_obj]->pos.y = read_number(in);
    o[g->n_obj]->pos.z = read_number(in);
  }
  else if(g->s == sym_size) {
    phrase(in);
    o[g->n_obj]->size.x = read_number(in);
    o[g->n_obj]->size.y = read_number(in);
    o[g->n_obj]->size.z = read_number(in);
  }
  else if(g->s == sym_rotation) {
    phrase(in);
    o[g->n_obj]->rotation = read_number(in);
  }
  // base color
  else if(g->s == sym_color && o[g->n_obj]->obj == BASE) {
    // Getting int
    g->isint = TRUE;
    phrase(in);
    o[g->n_obj]->b_color = g->ini;
    g->isint = FALSE;
    phrase(in);
  }
  else {
    printf("missing, phrase error or ???\n");
    phrase(in);
    //sleep(5);
  }

  }
  

  

}