Back to Evolution Project page
00001 #include "Level.h" 00002 #include <ClanLib/core.h> 00003 #include <ClanLib/magick.h> 00004 #include <stdio.h> 00005 00006 //#ifndef SPRITE 00007 #include "sprite.h" 00008 //#endif 00009 00010 void 00011 TLevel::Init(int width, int height) { //Constructor.. 00012 //Initial values for window position... 00013 window_x = 0; 00014 window_y = 0; 00015 window_width = 2; 00016 window_height = 2; 00017 offsetx = offsety = 0; 00018 init_variables(); 00019 read_config(); //Read the config file and load the list of sprites... 00020 level_width = width; 00021 level_height = height; 00022 resize_array_of_units(); 00023 read_images(); 00024 offsetx = offsety = 0; 00025 } 00026 00027 int 00028 TLevel::LoadFromFile(char *filename) { 00029 FILE *f; 00030 if (!file_exists(filename)) return 0; 00031 00032 f = fopen(filename, "rb"); 00033 if (!f) return 0; 00034 00035 if (!open_level_from_file(f)) { 00036 g_print ("File was opened but I have found errors on loading:'%s'!!\n", filename); 00037 return 0; 00038 } 00039 00040 fclose(f); 00041 return 1; 00042 } 00043 00044 int 00045 TLevel::read_images(void) { 00046 GList *last, *current; 00047 struct sprite *sp; 00048 CL_Surface *sf; 00049 00050 //Do it on grounds... 00051 last = g_list_first(sprite_list); 00052 current = g_list_last (sprite_list); 00053 if (last) { 00054 do { 00055 sp = (struct sprite *)(current->data); 00056 // g_print("ground description: '%s' Id:%d '%s'\n", sp->description, sp->id, sp->filename_tga); 00057 sf = CL_MagickProvider::create(sp->filename_tga, NULL); 00058 sp->pixmap = sf; 00059 if (current==last) break; 00060 current = current->prev; 00061 } while (1); 00062 } 00063 00064 //Do it on sprites 00065 last = g_list_first(sprite_list_sp); 00066 current = g_list_last (sprite_list_sp); 00067 if (last) { 00068 do { 00069 sp = (struct sprite *)(current->data); 00070 // g_print("ground description: '%s' Id:%d '%s'\n", sp->description, sp->id, sp->filename_tga); 00071 sf = CL_MagickProvider::create(sp->filename_tga, NULL); 00072 sp->pixmap = sf; 00073 if (current==last) break; 00074 current = current->prev; 00075 } while (1); 00076 } 00077 00078 return 1; 00079 } 00080 00081 int 00082 TLevel::Draw(void) { 00083 int i, j, offs; 00084 00085 //First draw ground 00086 for (j=0; j<window_height; j++) { 00087 offs = (j+offsety)*level_width+offsetx-1; 00088 for (i=0; i<window_width; i++) { 00089 offs++; 00090 if (level[offs].draw_here) 00091 if (level[offs].sp_unit){ 00092 ((CL_Surface *)(level[offs].sp_unit->sprite->pixmap))->put_screen( 00093 (window_x+i)*UNIT_WIDTH, 00094 (window_y+j)*UNIT_HEIGHT 00095 ); 00096 } 00097 } 00098 } 00099 00100 //Now draw sprites 00101 for (j=0; j<window_height; j++) { 00102 offs = (j+offsety)*level_width+offsetx-1; 00103 for (i=0; i<window_width; i++) { 00104 offs++; 00105 if (levelsp[offs].draw_here) 00106 if (levelsp[offs].sp_unit){ 00107 ((CL_Surface *)(levelsp[offs].sp_unit->sprite->pixmap))->put_screen( 00108 (window_x+i)*UNIT_WIDTH, 00109 (window_y+j)*UNIT_HEIGHT 00110 ); 00111 } 00112 } 00113 } 00114 00115 return 1; 00116 } 00117 00118 void 00119 TLevel::set_window(int x, int y, int w, int h) { 00120 window_x = x; 00121 window_y = y; 00122 window_width = w; 00123 window_height = h; 00124 } 00125 00126 void 00127 TLevel::set_offsetx(int ox) { 00128 offsetx = (ox<0) ? 0 : ((ox<level_width-window_width) ? ox : level_width-window_width); 00129 } 00130 void 00131 TLevel::set_offsety(int oy) { 00132 offsety = (oy<0) ? 0 : ((oy<level_height-window_height) ? oy : level_height-window_height); 00133 }