Back to Evolution Project page
00001 #include "auxiliar.h" 00002 #include <string.h> 00003 #include <gtk/gtk.h> 00004 00005 void toggle_view_grid(void) { 00006 view_grid = ! view_grid; 00007 draw_grid(); 00008 } 00009 00010 void hide_old(GdkDrawable *drawable) { 00011 int pixel_x, pixel_y; 00012 if ((old_x != -1) && (old_y != -1) && (sp_sel)) { 00013 pixel_x = (old_x)*UNIT_WIDTH; 00014 pixel_y = old_y*UNIT_HEIGHT + (sp_sel->sizey * UNIT_HEIGHT) - sp_sel->height_pixels; 00015 /* 00016 gdk_draw_pixmap(drawable, GcXOR, back_pixmap, pixel_x, pixel_y, 00017 pixel_x, pixel_y, sp_sel->width_pixels, sp_sel->height_pixels); 00018 */ 00019 draw_grid_local(old_x, old_y, old_x + sp_sel->sizex, old_y + sp_sel->sizey); 00020 } 00021 old_x = current_x; old_y = current_y; 00022 } 00023 00024 void show_current(GdkDrawable *drawable) { 00025 int pixel_x, pixel_y; 00026 if (!sp_sel) return; 00027 pixel_x = current_x*UNIT_WIDTH; 00028 pixel_y = current_y*UNIT_HEIGHT + (sp_sel->sizey * UNIT_HEIGHT) - sp_sel->height_pixels; 00029 gdk_draw_pixmap(drawable, defgc, sp_sel->pixmap, 0, 0, 00030 pixel_x, pixel_y, sp_sel->width_pixels, sp_sel->height_pixels); 00031 00032 } 00033 00034 void clear_pixmap(GdkPixmap *pixmap, GdkGC *gc) { 00035 int width, height; 00036 00037 gdk_window_get_size( (GdkWindow *) pixmap, &width, &height); 00038 gdk_draw_rectangle (pixmap, gc, TRUE, 0, 0, width, height); 00039 } 00040 00041 void draw_grid(void) { 00042 draw_grid_local(0, 0, level_width, level_height); 00043 } 00044 00045 void create_new_level(void) { 00046 frmNew = create_frmNew(); 00047 gtk_widget_show(frmNew); 00048 } 00049 00050 void create_my_frmNew(void) { 00051 if (level_changed) { 00052 dlg_areyousure = create_dlgAreYouSure(); 00053 gtk_widget_show(dlg_areyousure) ; 00054 return; 00055 } 00056 create_new_level(); 00057 } 00058 00059 00060 00061 void resize_map(void) { 00062 GtkWidget *drawing_area1; 00063 resize_array_of_units(); 00064 //Set the drawing area' size 00065 drawing_area1 = lookup_widget(Main, "drawarea"); 00066 gtk_widget_set_usize(drawing_area1, level_width * UNIT_WIDTH, level_height * UNIT_HEIGHT); 00067 gtk_widget_show(drawing_area1); 00068 00069 //Initialize back_pixmap 00070 if (back_pixmap) g_free(back_pixmap); 00071 // gdk_pixmap_destroy(back_pixmap); 00072 back_pixmap = gdk_pixmap_new (drawing_area1->window, level_width * UNIT_WIDTH, 00073 level_height * UNIT_HEIGHT, -1); 00074 00075 clear_pixmap(back_pixmap, drawing_area1->style->white_gc); 00076 old_x = old_y = -1; 00077 00078 //(Re)initialize GCs 00079 if (GcXOR) gdk_gc_destroy(GcXOR); 00080 GcXOR = gdk_gc_new(back_pixmap); 00081 gdk_gc_set_function(GcXOR, GDK_XOR); 00082 if (defgc) gdk_gc_destroy(defgc); 00083 defgc = gdk_gc_new(back_pixmap); 00084 00085 } 00086 00087 00088 void new_map(int sizex, int sizey) { 00089 level_width = sizex; 00090 level_height = sizey; 00091 resize_map(); 00092 refresh_all(); 00093 } 00094 00095 void refresh_all(void) { 00096 refresh_local(0, 0, level_width, level_height); 00097 } 00098 00099 void refresh_local(int x, int y, int x2, int y2) { 00100 int i, miny, minx; 00101 if (x<0) x=0; 00102 if (y<0) y=0; 00103 if (x2>level_width) x2=level_width; 00104 if (y2>level_height) y2=level_height; 00105 00106 minx=x; miny=y; 00107 for (i=x; i<x2; i++) 00108 if (levelsp[(y*level_width)+i].sp_unit) 00109 if (miny>levelsp[(y*level_width)+i].sp_unit->unity) 00110 miny=levelsp[(y*level_width)+i].sp_unit->unity; 00111 00112 for (i=y; i<y2; i++) 00113 if (levelsp[(i*level_width)+x].sp_unit) 00114 if (minx>levelsp[(i*level_width)+x].sp_unit->unitx) 00115 minx=levelsp[(i*level_width)+x].sp_unit->unitx; 00116 x = minx; y = miny; 00117 00118 draw_units_local(x, y, x2, y2); //First draw the units on the back_pixmap... 00119 00120 draw_grid_local(x, y, x2, y2); //And after this and the grid on the screen... 00121 } 00122 00123 void draw_grid_local(int x, int y, int x2, int y2) { 00124 int i; 00125 GtkWidget *drawarea; 00126 00127 if (x<0) x=0; 00128 if (y<0) y=0; 00129 if (x2>level_width) x2=level_width; 00130 if (y2>level_height) y2=level_height; 00131 00132 00133 drawarea = lookup_widget(Main, "drawarea"); 00134 00135 //Draw the back pixmap 00136 if (back_pixmap) { 00137 gdk_draw_pixmap (drawarea->window, drawarea->style->fg_gc[drawarea->state], back_pixmap, 00138 x*UNIT_WIDTH, y*UNIT_HEIGHT, 00139 x*UNIT_WIDTH, y*UNIT_HEIGHT, 00140 (x2-x)*UNIT_WIDTH, (y2-y)*UNIT_HEIGHT); 00141 } 00142 00143 //Draw the grid 00144 if (!level) return; 00145 if (view_grid) { 00146 for (i=y; i<=y2; i++){ 00147 gdk_draw_line(drawarea->window, drawarea->style->fg_gc[drawarea->state], 00148 x*UNIT_WIDTH, i*UNIT_HEIGHT, x2*UNIT_WIDTH, i*UNIT_HEIGHT); 00149 } 00150 for (i=x; i<=x2; i++){ 00151 gdk_draw_line(drawarea->window, drawarea->style->fg_gc[drawarea->state], 00152 i*UNIT_WIDTH, y*UNIT_WIDTH, i*UNIT_WIDTH, y2*UNIT_HEIGHT); 00153 } 00154 } 00155 00156 } 00157 00158 00159 void draw_units_local(int x, int y, int x2, int y2) { 00160 //Draw the back pixmap from the unit map... 00161 int i, j; 00162 sprite *sp; 00163 00164 //First, draw the grounds... 00165 for (i=x; i<x2; i++) 00166 for (j=y; j<y2; j++) 00167 if ((level[(j*level_height)+i].sp_unit) && (level[(j*level_height)+i].draw_here)) { 00168 sp = level[(j*level_height)+i].sp_unit->sprite; 00169 gdk_draw_pixmap (back_pixmap, defgc, 00170 (GdkPixmap *) sp->pixmap, 00171 0, 0, 00172 i*UNIT_WIDTH, j*UNIT_HEIGHT, 00173 sp->width_pixels, sp->height_pixels); 00174 } 00175 00176 //...after, draw the sprites. 00177 if (view_sprites) { 00178 for (i=x; i<x2; i++) 00179 for (j=y; j<y2; j++) 00180 if ((levelsp[(j*level_height)+i].sp_unit) && (levelsp[(j*level_height)+i].draw_here)) { 00181 sp = levelsp[(j*level_height)+i].sp_unit->sprite; 00182 gdk_draw_pixmap (back_pixmap, defgc, 00183 (GdkPixmap *) sp->pixmap, 00184 0, 0, 00185 i*UNIT_WIDTH, j*UNIT_HEIGHT, 00186 sp->width_pixels, sp->height_pixels); 00187 } 00188 } 00189 } 00190 00191 00192 00193 void draw_units_all(void) { 00194 draw_units_local(0, 0, level_width, level_height); 00195 } 00196 00197 00198 void event_on_unit(sprite *sp, int x, int y) { 00199 // g_print("%d %d \n", sp->type, sp->id); 00200 if ((!view_sprites) && (sp->type==SPRITE_TYPE_SPRITE)) return; 00201 00202 if ((sp->type==SPRITE_TYPE_SPRITE) && (sp->id==1)) 00203 delete_sprite_on_unit_le(levelsp, x, y); 00204 else 00205 add_sprite_on_unit_le(sp, x, y); 00206 00207 level_changed = TRUE; 00208 } 00209 00210 00211 void toggle_view_sprites(void) { 00212 view_sprites = !view_sprites; 00213 refresh_all(); 00214 } 00215 00216 void exit_program(void) { 00217 gtk_main_quit(); 00218 } 00219 00220 void show_on_statusbar(gchar *text) { 00221 GtkWidget *statusbar; 00222 statusbar = lookup_widget(Main, "statusbar1"); 00223 gtk_statusbar_pop (GTK_STATUSBAR(statusbar), 1); 00224 gtk_statusbar_push (GTK_STATUSBAR(statusbar), 1, text); 00225 } 00226 00227 void set_pixmaps_from_filenames(GList *sp_lst) { 00228 GList *last, *current; 00229 GtkWidget *window; 00230 GtkStyle *style; 00231 GdkBitmap *mask; 00232 sprite *sp; 00233 int width=0, height=0; 00234 00235 window = lookup_widget(Main, "Main"); 00236 style = gtk_widget_get_style(window); 00237 00238 last = g_list_first(sp_lst); 00239 current = g_list_last(sp_lst); 00240 00241 if (last) { 00242 do { 00243 sp = (sprite *)(current->data); 00244 sp->pixmap = gdk_pixmap_create_from_xpm (window->window, &mask, 00245 &style->bg[GTK_STATE_NORMAL], 00246 sp->filename); 00247 // g_print(" %s %s %p\n", sp->description, sp->filename, sp->pixmap); 00248 sp->mask = mask; 00249 gdk_window_get_size( (GdkWindow *)sp->pixmap, &width, &height); 00250 sp->width_pixels = width; 00251 sp->height_pixels = height; 00252 00253 if (current==last) break; 00254 current = current->prev; 00255 } while (1); 00256 } 00257 } 00258 00259 void delete_sprite_on_unit_le(unit *unitmap, int x, int y) { 00260 rect r; 00261 r = delete_sprite_on_unit(unitmap, x, y); 00262 refresh_local(r.x, r.y, r.sx, r.sy); 00263 } 00264 00265 void add_sprite_on_unit_le(sprite *sp, int x, int y){ 00266 rect r; 00267 r = add_sprite_on_unit(sp, x, y); 00268 refresh_local(r.x, r.y, r.sx, r.sy); 00269 } 00270 00271 00272 int open_level(gchar *fn) { 00273 FILE *f; 00274 file_header fh; 00275 00276 g_print("Opening file '%s'\n", fn); 00277 f = fopen(fn, "rb"); 00278 if (!f) return 0; 00279 00280 //Read the reader first... 00281 fread(&fh, sizeof(fh), 1, f); 00282 level_width = fh.level_width; 00283 level_height = fh.level_height; 00284 resize_map(); 00285 fseek(f, 0, SEEK_SET); 00286 00287 if (!open_level_from_file(f)) { 00288 show_on_statusbar("Error loading file!!!"); 00289 fclose(f); 00290 return 0; 00291 } 00292 fclose(f); 00293 refresh_all(); 00294 strcpy(current_filename, fn); 00295 level_changed=FALSE; 00296 return 1; 00297 } 00298 00299 int save_level(gchar *fn) { 00300 FILE *f; 00301 if (!g_strcasecmp(fn, "")) 00302 return 0; 00303 g_print("Saving file '%s'\n", fn); 00304 f = fopen(fn, "wb"); 00305 if (!f) return 0; 00306 if (!save_level_to_file(f)) { 00307 fclose(f); 00308 show_on_statusbar("Error saving file!!!"); 00309 return 0; 00310 } 00311 fclose(f); 00312 strcpy(current_filename, fn); 00313 level_changed=FALSE; 00314 return 1; 00315 }