static void set_debug_type (void) { char cfgbuf[CONFIG_MAX]; int ret; ret = get_config(pal_state.root_config, "loader.debug_type", cfgbuf, CONFIG_MAX); if (ret <= 0) return; PAL_HANDLE handle = NULL; if (!memcmp(cfgbuf, "inline", 7)) { ret = _DkStreamOpen(&handle, "dev:tty", PAL_ACCESS_RDWR, 0, 0, 0); if (ret < 0) init_fail(-ret, "cannot open debug stream"); } else if (!memcmp(cfgbuf, "file", 5)) { ret = get_config(pal_state.root_config, "loader.debug_file", cfgbuf, CONFIG_MAX); if (ret <= 0) init_fail(PAL_ERROR_INVAL, "debug file not specified"); ret = _DkStreamOpen(&handle, cfgbuf, PAL_ACCESS_RDWR, PAL_SHARE_OWNER_R|PAL_SHARE_OWNER_W, PAL_CREAT_TRY, 0); if (ret < 0) init_fail(-ret, "cannot open debug file"); } else if (memcmp(cfgbuf, "none", 5)) { init_fail(PAL_ERROR_INVAL, "unknown debug type"); } __pal_control.debug_stream = handle; }
static void load_libraries (void) { /* we will not make any assumption for where the libraries are loaded */ char cfgbuf[CONFIG_MAX]; int len, ret = 0; /* loader.preload: any other libraries to preload. The can be multiple URIs, seperated by commas */ len = get_config(pal_state.root_config, "loader.preload", cfgbuf, CONFIG_MAX); if (len <= 0) return; char * c = cfgbuf, * library_name = c; for (;; c++) if (*c == ',' || !(*c)) { if (c > library_name) { #if PROFILING == 1 unsigned long before_load_library = _DkSystemTimeQuery(); #endif *c = 0; if ((ret = load_elf_object(library_name, OBJECT_PRELOAD)) < 0) init_fail(-ret, "Unable to load preload library"); #if PROFILING == 1 pal_state.linking_time += _DkSystemTimeQuery() - before_load_library; #endif } if (c == cfgbuf + len) break; library_name = c + 1; } }
int main(int argc, char const *argv[]) { int i = 0; int t = 1; int n_mostros = 5; bool click = false; bool nova_horda = true; bool render = false; bool torre_mouse = false; char a; char b; int r; int l; Sistema sistema; Torre torre[10]; Tiro tiro[10]; Monstro monstro[n_mostros]; Coord coordenada[A*B]; //Declara�ao vair�veis allegro ALLEGRO_DISPLAY *janela = NULL; //Vari�vel para a janela ALLEGRO_EVENT_QUEUE *fila_eventos = NULL; // '' para eventos ALLEGRO_BITMAP *imagem = NULL; // '' para imagem ALLEGRO_TIMER *timer = NULL; // '' para o tempo (fps) ALLEGRO_FONT *fonte = NULL; // '' para fonte //Inicializa o allegro, mouse e add-ons al_init(); al_install_mouse(); al_init_primitives_addon(); al_init_image_addon(); al_init_font_addon(); al_init_ttf_addon(); a_coord(coordenada, fonte); init_horda(monstro, n_mostros); init_system(sistema); initTorre(torre, tiro, t-1); //Atribui atributos às variáveis allegro janela = al_create_display(LARGURA_TELA, ALTURA_TELA); fila_eventos = al_create_event_queue(); imagem = al_load_bitmap("virus.png"); timer = al_create_timer(1.0 / fps); fonte = al_load_font("arial.ttf", 12, 0); //Fonte DejaVu //Inicializa o mouse e tempo al_set_system_mouse_cursor(janela, ALLEGRO_SYSTEM_MOUSE_CURSOR_DEFAULT); al_start_timer(timer); al_install_keyboard(); init_fail(janela, fonte, fila_eventos, imagem, timer); //Fun�ao de teste //Regista os eventos da janela, mouse e timer na vari�vel de eventos (fila_eventos) al_register_event_source(fila_eventos, al_get_display_event_source(janela)); al_register_event_source(fila_eventos, al_get_mouse_event_source()); al_register_event_source(fila_eventos, al_get_keyboard_event_source()); al_register_event_source(fila_eventos, al_get_timer_event_source(timer)); al_clear_to_color(al_map_rgb(235, 235, 235)); //Limpa a tela al_flip_display(); //Atualiza a tela //Loop principal while (!GameOver) { ALLEGRO_EVENT evento; //Variavel para eventos al_wait_for_event(fila_eventos, &evento); if(evento.type == ALLEGRO_EVENT_TIMER) //Evento de renderiza�ao { i++; render = true; FireTiro(tiro, torre, monstro, t-1); UpdateTiro(tiro, monstro, t-1); update_horda(monstro, mapa, n_mostros); colisao_horda(tiro, monstro, n_mostros); } if (evento.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { GameOver = true; } else if(evento.type == ALLEGRO_EVENT_MOUSE_AXES) { pos_x = evento.mouse.x; pos_y = evento.mouse.y; a = coordenada[(pos_x/l_celula)].letra[0]; b = coordenada[(pos_x/l_celula)].letra[1]; l = coordenada[(pos_y/a_celula)].numero; r = conversao_coordenadas(coordenada, a, b); } else if(evento.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) { if (mapa[pos_y/a_celula][pos_x/l_celula] == 9) { torre_mouse = true; torre[t].in_mouse = true; } if(torre_mouse && !click) { if (mapa[pos_y/a_celula][pos_x/l_celula] != 9) { mapa[pos_y/a_celula][pos_x/l_celula] = 10; torre_mouse = false; torre[t].in_mouse = false; t++; } } click = !click; } else if(evento.type == ALLEGRO_EVENT_KEY_DOWN) { switch(evento.keyboard.keycode) { case ALLEGRO_KEY_SPACE: start_horda(monstro, n_mostros); break; } } else if(render && al_is_event_queue_empty(fila_eventos)) { render = false; coor_matrix(mapa, coordenada, fonte); al_draw_textf(fonte, al_map_rgb(0, 0, 0), LARGURA_TELA/4, 50, ALLEGRO_ALIGN_CENTRE, "Taxa de Frames: %i", i); al_draw_textf(fonte, al_map_rgb(0, 0, 0), pos_x, pos_y, ALLEGRO_ALIGN_LEFT, " x:%i y:%i", pos_x, pos_y); draw_horda(monstro, n_mostros, imagem); if(torre_mouse) { draw_tower(r, l, torre, t); } drawTiro(tiro, t-1); al_flip_display(); al_clear_to_color(al_map_rgb(255,255,255)); } } destroy_al(janela, fonte, fila_eventos, imagem, timer); //Destroi as vari�veis allegro return 0; }
void pal_main (PAL_NUM pal_token, void * pal_addr, const char * pal_name, int argc, const char ** argv, const char ** envp, PAL_HANDLE parent_handle, PAL_HANDLE thread_handle, PAL_HANDLE exec_handle, PAL_HANDLE manifest_handle) { int ret; bool is_parent = !parent_handle; #if PROFILING == 1 __pal_control.host_specific_startup_time = _DkSystemTimeQuery() - pal_state.start_time; #endif pal_state.pal_token = pal_token; pal_state.pal_addr = pal_addr; pal_state.parent_handle = parent_handle; pal_state.pagesize = _DkGetPagesize(); pal_state.alloc_align = _DkGetAllocationAlignment(); pal_state.alloc_shift = pal_state.alloc_align - 1; pal_state.alloc_mask = ~pal_state.alloc_shift; init_slab_mgr(pal_state.alloc_align); char * exec = NULL, * manifest = NULL; if (exec_handle) { exec = __alloca(URI_MAX); ret = _DkStreamGetName(exec_handle, exec, URI_MAX); if (ret < 0) init_fail(-ret, "cannot get executable name"); } if (manifest_handle) { manifest = __alloca(URI_MAX); ret = _DkStreamGetName(manifest_handle, manifest, URI_MAX); if (ret < 0) init_fail(-ret, "cannot get manifest name"); } else { if (is_parent) { #if PROFILING == 1 unsigned long before_find_manifest = _DkSystemTimeQuery(); #endif do { if (exec_handle) { assert(!!exec); /* try open "<exec>.manifest" */ manifest = __alloca(URI_MAX); snprintf(manifest, URI_MAX, "%s.manifest", exec); ret = _DkStreamOpen(&manifest_handle, manifest, PAL_ACCESS_RDONLY, 0, 0, 0); if (!ret) break; } /* try open "file:manifest" */ manifest = "file:manifest"; ret = _DkStreamOpen(&manifest_handle, manifest, PAL_ACCESS_RDONLY, 0, 0, 0); if (!ret) break; /* well, there is no manifest file, leave it alone */ if (!manifest_handle) printf("Can't fine any manifest, will run without one\n"); } while (0); #if PROFILING == 1 pal_state.manifest_loading_time += _DkSystemTimeQuery() - before_find_manifest; #endif } } /* load manifest if there is one */ if (manifest_handle) { #if PROFILING == 1 unsigned long before_load_manifest = _DkSystemTimeQuery(); #endif PAL_STREAM_ATTR attr; ret = _DkStreamAttributesQuerybyHandle(manifest_handle, &attr); if (ret < 0) init_fail(-ret, "cannot open manifest file"); void * cfg_addr = NULL; int cfg_size = attr.pending_size; ret = _DkStreamMap(manifest_handle, &cfg_addr, PAL_PROT_READ, 0, ALLOC_ALIGNUP(cfg_size)); if (ret < 0) init_fail(-ret, "cannot open manifest file"); struct config_store * root_config = malloc(sizeof(struct config_store)); root_config->raw_data = cfg_addr; root_config->raw_size = cfg_size; root_config->malloc = malloc; root_config->free = free; const char * errstring = NULL; if ((ret = read_config(root_config, loader_filter, &errstring)) < 0) init_fail(-ret, errstring); pal_state.root_config = root_config; #if PROFILING == 1 pal_state.manifest_loading_time += _DkSystemTimeQuery() - before_load_manifest; #endif } /* if there is no executable, try to find one in the manifest */ if (is_parent && !exec_handle) { exec = __alloca(URI_MAX); assert(!!pal_state.root_config); ret = get_config(pal_state.root_config, "loader.exec", exec, URI_MAX); if (ret > 0) { ret = _DkStreamOpen(&exec_handle, exec, PAL_ACCESS_RDONLY, 0, 0, 0); if (ret < 0) init_fail(-ret, "cannot open executable"); /* must be a ELF */ if (check_elf_object(exec_handle) < 0) init_fail(PAL_ERROR_INVAL, "executable is not a ELF binary"); } else { exec = NULL; } } if (is_parent && !exec_handle && !manifest_handle) { printf("USAGE: %s [executable|manifest] args ...\n", pal_name); _DkProcessExit(0); return; } pal_state.manifest = manifest; pal_state.manifest_handle = manifest_handle; pal_state.exec = exec; pal_state.exec_handle = exec_handle; const char * first_argv = *argv; argc--; argv++; if (is_parent && exec_handle) { first_argv = exec; if (pal_state.root_config) { char cfgbuf[CONFIG_MAX]; ret = get_config(pal_state.root_config, "loader.execname", cfgbuf, CONFIG_MAX); if (ret > 0) first_argv = remalloc(cfgbuf, ret + 1); } } if (pal_state.root_config) load_libraries(); if (exec_handle) { #if PROFILING == 1 unsigned long before_load_exec = _DkSystemTimeQuery(); #endif ret = load_elf_object_by_handle(exec_handle, OBJECT_EXEC); if (ret < 0) init_fail(ret, PAL_STRERROR(ret)); #if PROFILING == 1 pal_state.linking_time += _DkSystemTimeQuery() - before_load_exec; #endif } #if PROFILING == 1 unsigned long before_tail = _DkSystemTimeQuery(); #endif if (pal_state.root_config) { read_envs(&envp); set_debug_type(); set_syscall_symbol(); } __pal_control.process_id = _DkGetProcessId(); __pal_control.host_id = _DkGetHostId(); __pal_control.manifest_handle = manifest_handle; __pal_control.executable = exec; __pal_control.parent_process = parent_handle; __pal_control.first_thread = thread_handle; _DkGetAvailableUserAddressRange(&__pal_control.user_address.start, &__pal_control.user_address.end); __pal_control.pagesize = pal_state.pagesize; __pal_control.alloc_align = pal_state.alloc_align; __pal_control.broadcast_stream = _DkBroadcastStreamOpen(); _DkGetCPUInfo(&__pal_control.cpu_info); __pal_control.mem_info.mem_total = _DkMemoryQuota(); #if PROFILING == 1 pal_state.tail_startup_time += _DkSystemTimeQuery() - before_tail; __pal_control.relocation_time = pal_state.relocation_time; __pal_control.linking_time = pal_state.linking_time; __pal_control.manifest_loading_time = pal_state.manifest_loading_time; __pal_control.allocation_time = pal_state.slab_time; __pal_control.child_creation_time = is_parent ? 0 : pal_state.start_time - pal_state.process_create_time; #endif /* Now we will start the execution */ start_execution(first_argv, argc, argv, envp); /* We wish we will never reached here */ init_fail(PAL_ERROR_DENIED, "unexpected termination"); }
int main(int argc, char const *argv[]) { int n_hordas = 0; //Numero de hordas chamadas bool nova_horda = true; //Chama nova horda bool torre_mouse = false; //Se a torre está no mouse bool info_torre = false; //Chama a funçao de informaçoes da torre bool compra_torre = false; //Exibe as informaçoes da torre a ser comprada bool upgrade_torre; //Guarda os upgrades da torre] int tower_posx = 0; //Posiçao x de determinada torre int tower_posy = 0; //Posiçao y de determinada torre int torre_ID; //Identifica as torres int t = 0; //Contagem das torres int t_1, t_2; //Contagem para disparo int r; //Variável para colunas int l; //Variável para linhas bool render = false; //Renderizaçao int resposta = 0; //Resposta se os monstros estão todos mortos int gamestate = 0; //Gamestates //Setup inicial Sistema sistema; Monstro monstro[tipos_monstros][n_monstros]; Tipo tipo_torre; Tipo tipo1; Tipo tipo2; Tipo upgrade1_torre1; Torre torre[100]; //Declaracao vairaveis allegro ALLEGRO_DISPLAY *janela = NULL; //Vari�vel para a janela ALLEGRO_EVENT_QUEUE *fila_eventos = NULL; // '' para eventos ALLEGRO_BITMAP *imagem = NULL; // '' para imagem ALLEGRO_TIMER *timer = NULL; // '' para o tempo (fps) ALLEGRO_FONT *fonte = NULL; // '' para fonte ALLEGRO_BITMAP *trilha = NULL; ALLEGRO_BITMAP *fundao = NULL; ALLEGRO_BITMAP *spawn = NULL; ALLEGRO_BITMAP *the_end = NULL; ALLEGRO_BITMAP *monstro2 = NULL; ALLEGRO_BITMAP *torre1 = NULL; ALLEGRO_FONT *fonte40 = NULL; //Inicializa o allegro, mouse e add-ons al_init(); al_install_mouse(); al_init_primitives_addon(); al_init_image_addon(); al_init_font_addon(); al_init_ttf_addon(); //Setup inicial do sistema, monstros e torres init_horda(monstro, n_monstros, n_hordas, tipos_monstros); init_system(sistema); setup_torre1(tipo1); setup_torre2(tipo2); upgrade1_tower1(upgrade1_torre1); //Atribui atributos às variáveis allegro janela = al_create_display(LARGURA_TELA, ALTURA_TELA); fila_eventos = al_create_event_queue(); imagem = al_load_bitmap("virus.jpg"); trilha = al_load_bitmap("fundoc.jpg"); fundao = al_load_bitmap("fundod.jpg"); spawn = al_load_bitmap("spawn.jpg"); the_end = al_load_bitmap("the end.jpg"); monstro2 = al_load_bitmap("virus2.jpg"); torre1 = al_load_bitmap("halter.png"); timer = al_create_timer(1.0 / fps); fonte = al_load_font("arial.ttf", 12, 0); fonte40 = al_load_font("arial.ttf", 40, 0); //Inicializa o mouse e tempo al_set_system_mouse_cursor(janela, ALLEGRO_SYSTEM_MOUSE_CURSOR_DEFAULT); al_start_timer(timer); al_install_keyboard(); init_fail(janela, fonte, fila_eventos, imagem, timer, trilha); //Fun�ao de teste de inicializaçao do allegro //Regista os eventos da janela, mouse e timer na vari�vel de eventos (fila_eventos) al_register_event_source(fila_eventos, al_get_display_event_source(janela)); al_register_event_source(fila_eventos, al_get_mouse_event_source()); al_register_event_source(fila_eventos, al_get_keyboard_event_source()); al_register_event_source(fila_eventos, al_get_timer_event_source(timer)); al_clear_to_color(al_map_rgb(235, 235, 235)); //Limpa a tela al_flip_display(); //Atualiza a tela //Loop principal while (!GameOver) { ALLEGRO_EVENT evento; //Variavel para eventos al_wait_for_event(fila_eventos, &evento); //Espera por eventos if (evento.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { GameOver = true; } switch(gamestate) { case 0: //Menu inicial { if(evento.type == ALLEGRO_EVENT_TIMER) { render = true; } if(evento.type == ALLEGRO_EVENT_KEY_DOWN) { switch(evento.keyboard.keycode) { case ALLEGRO_KEY_ENTER: gamestate = 1; break; case ALLEGRO_KEY_BACKSPACE: gamestate = 2; break; } } break; } case 1: //Jogo { if(evento.type == ALLEGRO_EVENT_TIMER) { if(info_torre) { if (torre[torre_ID].upgrade == 0 && sistema.money >= 60) { mapa[25][30] = 12; } } if(!info_torre) { mapa[25][30] = 0; } for(int j = 0; j < t; j++) //Loop para o disparo das torres { if(torre[j].n == 1) { if(t_1 >= fps*(torre[j].fire_rate)) { fire_tiro(torre, monstro, t, n_monstros, tipos_monstros); //Dispara tiros t_1 = 0; } } if(torre[j].n == 2) { if(t_2 >= fps*(torre[j].fire_rate)) { fire_tiro(torre, monstro, t, n_monstros, tipos_monstros); //Dispara tiros t_2 = 0; } } } update_horda(monstro, sistema, mapa, n_monstros, tipos_monstros); update_tiro(torre, monstro, t, n_monstros, tipos_monstros); colisao_horda(torre, monstro, t, n_monstros, sistema, &resposta, tipos_monstros); t_1++; t_2++; render = true; if(sistema.lives <= 0) gamestate = 2; } else if(evento.type == ALLEGRO_EVENT_MOUSE_AXES) { pos_x = evento.mouse.x; //Armazena a posiçao x do mouse pos_y = evento.mouse.y; //Armazena a posiçao y do mouse r = pos_x/l_celula; // Atribui uma celula de coluna l = pos_y/a_celula; // Atribui uma celula de linha } else if(evento.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) { switch (mapa[l][r]) { case 10: info_torre = false; compra_torre = true; tipo_torre = tipo1; if(sistema.money >= tipo_torre.price && evento.mouse.button & 1) torre_mouse = true; break; case 20: info_torre = false; compra_torre = true; tipo_torre = tipo2; if(sistema.money >= tipo_torre.price && evento.mouse.button & 1) torre_mouse = true; break; case 11: torre_ID = find_tower_ID(torre, t, r, l); info_torre = true; break; case 12: torre_ID = find_tower_ID(torre, t, r, l); info_torre = true; break; case 21: torre_ID = find_tower_ID(torre, t, r, l); info_torre = true; break; default: info_torre = false; } if(torre_mouse && (mapa[l][r] == 0 || mapa[l][r] == 5) && evento.mouse.button & 1) //Posicionamento da torre enquanto ela estiver no mouse { setup_tower(torre, tipo_torre, t, r, l); sistema.money -= tipo_torre.price; //Pagamento da torre torre_mouse = false; compra_torre = false; t++; } if(torre_mouse && evento.mouse.button & 2) //Cancela compra { torre_mouse = false; compra_torre = false; } if(compra_torre && mapa[l][r] != 10 && mapa[l][r] != 20) //Termina a exibiçao da torre a ser comprada { compra_torre = false; } if(info_torre && mapa[l][r] == 12 ) { sistema.money -= 60; torre_ID = find_tower_ID(torre, t, r, l); upgrade_tower(torre, upgrade1_torre1, torre_ID); //upgrade_torre = true; } } else if(evento.type == ALLEGRO_EVENT_KEY_DOWN) { printf("resposta = %d\n", resposta); if(resposta == 1 || n_hordas == 0) { switch(evento.keyboard.keycode) { case ALLEGRO_KEY_SPACE: //Inicializa uma nova horda start_horda(monstro, n_monstros, n_hordas, tipos_monstros); n_hordas++; break; } } } } break; case 2: //Fim de jogo { if(evento.type == ALLEGRO_EVENT_TIMER) { render = true; } if(evento.type == ALLEGRO_EVENT_KEY_DOWN) { switch(evento.keyboard.keycode) { case ALLEGRO_KEY_R: init_system(sistema); init_horda(monstro, n_monstros, n_hordas, tipos_monstros); restart_tower(torre, t); n_hordas = 0; setup_array(mapa); gamestate = 1; break; case ALLEGRO_KEY_ESCAPE: GameOver = true; break; } } break; } } if(render && al_is_event_queue_empty(fila_eventos)) { render = false; if(gamestate == 0) { al_clear_to_color(al_map_rgb(255,255,255)); al_draw_textf(fonte, al_map_rgb(0, 0, 255), LARGURA_TELA/2, (ALTURA_TELA/2) - 20, 0, "Pressione ENTER para Jogar"); al_draw_textf(fonte, al_map_rgb(0, 0, 0), LARGURA_TELA/2, (ALTURA_TELA/2) + 20, 0, "Pressione BACKSPACE para Sair"); } if(gamestate == 1) { al_clear_to_color(al_map_rgb(61, 10, 10)); draw_background(mapa, fonte, trilha, fundao, spawn, the_end); //Desenha o plano de fundo draw_towers(mapa, sistema, fonte, the_end, torre1); //Desenha as torres al_draw_textf(fonte, al_map_rgb(255, 255, 255), 900, 15, ALLEGRO_ALIGN_LEFT, "Vidas do sistema %i", sistema.lives); al_draw_textf(fonte, al_map_rgb(255, 255, 255), 900, 35, ALLEGRO_ALIGN_LEFT, "Bitcoins %.2f", sistema.money); al_draw_textf(fonte, al_map_rgb(255, 255, 255), 100, 15, ALLEGRO_ALIGN_LEFT, "Monstros mortos: %i Wave: %i", sistema.score, n_hordas); /* Mouse debug al_draw_textf(fonte, al_map_rgb(0, 0, 0), pos_x, pos_y, ALLEGRO_ALIGN_LEFT, "l:%i r:%i", l, r); al_draw_textf(fonte, al_map_rgb(0, 0, 0), pos_x, pos_y + 15, ALLEGRO_ALIGN_CENTRE, "mapa[l][r]: %i", mapa[l][r]); */ draw_horda(monstro, n_monstros, imagem, tipos_monstros, monstro2); //Desenha os montros if(torre_mouse) { draw_mouse_tower(r, l, tipo_torre); //Desenha a torre somente enquanto ela estiver no mouse } if(info_torre) { show_tower_information(torre, torre_ID, fonte); //info torres if (mapa[25][30] == 12) { al_draw_filled_circle(25 * l_celula + (l_celula/2), 30 * a_celula + (a_celula/2), l_celula/2, al_map_rgb(40, 150, 10)); } } if(compra_torre) { buy_tower(tipo_torre, fonte); //Exibe as informaçoes da torre a ser comprada } draw_tiro(torre, t); //Desenha os tiros } if(gamestate == 2) { al_clear_to_color(al_map_rgb(255,255,255)); al_draw_textf(fonte40, al_map_rgb(255, 0, 0), LARGURA_TELA/2, (ALTURA_TELA/2) - 100, 0, "Game Over"); al_draw_textf(fonte, al_map_rgb(0, 0, 0), LARGURA_TELA/2, (ALTURA_TELA/2) - 20, 0, "Pressione R para Jogar Novamente"); al_draw_textf(fonte, al_map_rgb(0, 0, 0), LARGURA_TELA/2, (ALTURA_TELA/2) + 20, 0, "Pressione ESC para Sair"); } al_flip_display(); } } destroy_al(janela, fonte, fila_eventos, imagem, timer); //Destroi as vari�veis allegro return 0; }