void init_game() { WINDOW* map_win = newwin(24, 80, 0, 0); WINDOW* area_win = newwin(6, 10, 0, 80); WINDOW* stats_win = newwin(8, 10, 6, 80); WINDOW* examine_win = newwin(10, 10, 14, 80); WINDOW* hp_win = newwin(1, 90, 24, 0); WINDOW* log_win = newwin(10, 90, 25, 0); dead = false; init_log(log_win); init_map(map_win); init_items(); world = calloc(LEVEL_COUNT, sizeof(map*)); world[0] = load_map("data/maps/farm.map", 78, 22, false, true); for(int i = 1; i < 3; ++i) world[i] = load_map("data/maps/easy_cave.map", 80 + 10 * (i - 1), 24 + 4 * (i - 1), true, true); for(int i = 3; i < 5; ++i) world[i] = load_map("data/maps/mid_cave.map", 80 + 10 * (i - 1), 24 + 4 * (i - 1), true, true); for(int i = 5; i < 7; ++i) world[i] = load_map("data/maps/hard_cave.map", 80 + 15 * (i - 1), 24 + 6 * (i - 1), true, true); for(int i =7; i < 9; ++i) world[i] = load_map("data/maps/crazy_cave.map", 80 + 18 * (i - 1), 24 + 8 * (i - 1), true, true); world[LEVEL_COUNT - 1] = load_map("data/maps/final.map", 78, 22, true, false); init_player(map_win, stats_win, hp_win, area_win, examine_win, world[0]); int x, y; get_random_empty_tile(&x, &y, world[LEVEL_COUNT - 1]); spawn_item(x, y, "data/items/cat.item", world[LEVEL_COUNT - 1]); get_random_empty_tile(&x, &y, world[0]); player_set_position(x, y); draw_map(x, y, world[0]); add_message(COLOR_DEFAULT, "@ symbol, a brave young farmer, was out for a stroll on his farm when his cat Cuddles ran down into the gaping starcase to the deadly Caves of Consternation! @ symbol had been meaning to patch that up for a while, but hadn't gotten a chance yet. Don't judge."); if(ask_question(COLOR_SELECTION, "Will you help @ symbol retrieve his cat, Cuddles?")) add_message(COLOR_HP_GOOD, "Excellent! Get to it, then!"); else { add_message(COLOR_HP_CRIT, "Well that was unexpected. Okay then, press q to quit to the main menu."); dead = true; } draw_log(); }
void wav(const char* filename) { extern rt_uint32_t current_offset; int fd; rt_err_t result; WAVEFORMATEX winfo; char buff[60]; s_BuffIndex = 0; fd = open(filename, O_RDONLY, 0); if (fd >= 0) { rt_size_t len; result = rt_sem_init(&s_semForPlay, "semforplay", 1, RT_IPC_FLAG_FIFO); if (result != RT_EOK) { rt_kprintf(" semaphore: semforplay init failed./r/n"); rt_sem_detach(&s_semForPlay);/* 脱离信号量对象 */ } len = read(fd, (char*)buff, 0x2c); if (strncmp("WAVEfmt", (char *)buff + 8, 7) != 0 ) {//非wav格式咱不理他 rt_kprintf("format is not support !\n\r"); close(fd); return; } memset(&winfo, 0, sizeof(winfo)); //从音频数据的起始抓取 信息,用于播放波特率设置等 winfo.wFormatTag = 0x01; //表示pcm编码 winfo.nChannels = *(rt_uint16_t *)(buff+0x16); winfo.nSamplesPerSec = *(rt_uint32_t *)(buff+0x18); winfo.nAvgBytesPerSec = *(rt_uint32_t *)(buff+0x1c); winfo.nBlockAlign = *(rt_uint16_t *)(buff+0x20); winfo.wBitsPerSample = *(rt_uint16_t *)(buff+0x22); winfo.cbSize = *(rt_uint32_t *)(buff+0x28); rt_kprintf("\nSample Rate = %d, Channels = %d, BitsPerSample = %d, file size = %dM Bytes\n", winfo.nSamplesPerSec, winfo.nChannels, winfo.wBitsPerSample, winfo.cbSize/1024/1024); if (winfo.wBitsPerSample!=16)//2byte { rt_kprintf("8bit format is not support !\n\r"); close(fd); return; } current_offset = 0; len = read(fd, (char *)(&BufData[s_BuffIndex][0]), (2048*2)); IIS_Config(winfo.nSamplesPerSec, (u32)(&BufData[0][0]), (2048)); do { if (player_is_playing() != RT_TRUE) { break; } result = rt_sem_take(&s_semForPlay, RT_WAITING_FOREVER); if (result != RT_EOK) { rt_kprintf(" semaphore: semforplay init failed./r/n"); rt_sem_detach(&s_semForPlay);/* 脱离信号量对象 */ } DMA_Transmit((u32)((&BufData[s_BuffIndex][0])), (2048)); if (++s_BuffIndex>=2) s_BuffIndex = 0; len = read(fd, (char *)(&BufData[s_BuffIndex][0]), (2048*2)); if (len != (2048*2)) { rt_kprintf("file read fail or there is no more data!\n\r"); close(fd); return; } current_offset+=len; if (player_get_mode() != PLAYER_PLAY_RADIO) player_set_position(current_offset); } while (len != 0); close(fd); } }
int main (int argc, char **argv) { // variables SDL_Surface *screen; // pantalla SDL_Event event; // para los eventos de SDL (teclado y/o mouse) Uint8 *keystates = SDL_GetKeyState(NULL); // para detectar teclas mantenidas presionadas Uint8 salir = 0; // para determinar cuando se cierra la ventana int mouse_x=0, mouse_y=0; // coordenadas del mouse char info1[100]; // texto para mostrar informacion variada linea 1 char info2[100]; // texto para mostrar informacion variada linea 2 short int time; // variable para la hora short int ticks; // variable para mover elementos independiente de los FPS // cargar configuracion struct conf *conf = conf_load(); if (!conf) return EXIT_FAILURE; // verificar a donde nos queremos conectar // se indicó el servidor como 1er parametro if(argc>=2) { free(conf->SERVER); conf->SERVER = malloc(strlen(argv[1])); strcpy(conf->SERVER, argv[1]); } // se indicó el puerto como 2do parametro if(argc>=3) { conf->PORT = atoi(argv[2]); } // conectar al servidor int connection = client_init(conf); if (!connection) return EXIT_FAILURE; // buffer para transferencia entre servidor y cliente char buffer[BUFFER_SIZE]; // obtener id del jugador int playerID = atoi(query("get;id", connection)); // recibir y actualizar configuraciones desde el servidor conf_update(conf, query("get;conf", connection)); // inicializar sistema sdl_init(&screen); // cargar fuente TTF_Font *fuente = ttf_load(FONT_NAME, FONT_SIZE, FONT_STYLE); // cargar todos los posibles objetos de un mapa MapObject *mapobjects = mapobjects_load(screen); if (!mapobjects) return EXIT_FAILURE; // cargar mapa char **map = map_open(conf->MAP); if (!map) return EXIT_FAILURE; // cargar armas Weapon *weapons = weapons_load(); if (!weapons) return EXIT_FAILURE; // crear personaje jugador Player *pj = player_create(playerID, conf->PLAYER_NAME, conf->PLAYER_CHARACTER, screen, weapons, conf); // asignar armas al personaje player_change_weapon(&pj, conf->WEAPON_PRIMARY, 1); player_change_weapon(&pj, conf->WEAPON_SECONDARY, 2); // ubicar personaje en el mapa player_set_position(&pj, map, -1, -1, mapobjects); // informar estado del usuario bzero(buffer, sizeof(buffer)); sprintf(buffer, "info;%s;%s", conf->PLAYER_CHARACTER, conf->PLAYER_NAME); write(connection, buffer, strlen(buffer)); // crear bots y ubicar en el mapa Player *bots = bots_create(screen, weapons, conf); int i; for(i=0; i<conf->BOTS; ++i) { Player *aux = &bots[i]; player_set_position(&aux, map, -1, -1, mapobjects); } // guardar ticks ticks = SDL_GetTicks(); // ciclo infinito para refrescar la imagen de la pantalla while (!salir) { // solicitar informacion de todos los jugadores (menos el mio) char *enemies = query("get;players", connection); // enviar posicion del jugador al servidor sprintf(buffer, "position;%d;%d", (int)(pj->y), (int)(pj->x)); write(connection, buffer, strlen(buffer)); // time time = SDL_GetTicks()/1000; // dibujar mapa map_draw(screen, map, mapobjects, conf->BACKGROUND); // revisar si existen eventos de SDL while (SDL_PollEvent(&event)) { // salir del programa if(event.type==SDL_QUIT) salir = 1; // si se presiona una tecla else if(event.type==SDL_KEYDOWN) { // cambiar a pantalla completa if(event.key.keysym.sym==SDLK_f) SDL_WM_ToggleFullScreen(screen); // salir del juego else if(event.key.keysym.sym==SDLK_ESCAPE) salir = 1; // cambiar arma primaria else if(event.key.keysym.sym>=48&&event.key.keysym.sym<=57) player_change_weapon(&pj, event.key.keysym.sym-48, 1); } // si se mueve el mouse else if(event.type==SDL_MOUSEMOTION) { // guardar coordenadas del mouse mouse_x = event.motion.x; mouse_y = event.motion.y; } // si se presiona el mouse else if(event.type == SDL_MOUSEBUTTONDOWN ) { // guardar coordenadas del mouse mouse_x = event.button.x; mouse_y = event.button.y; // resetear flag de armas disparadas pj->primary->fired = 0; pj->secondary->fired = 0; // si se presiono el boton izquierdo if(event.button.button==SDL_BUTTON_LEFT && pj->primary->ammo) { Mix_PlayChannel(-1, pj->primary->sound, 0); pj->primary->fired = pj->primary->ammo--; } // si se presiono el boton derecho else if(event.button.button==SDL_BUTTON_RIGHT && pj->secondary->ammo) { Mix_PlayChannel(-1, pj->secondary->sound, 0); pj->secondary->fired = pj->secondary->ammo--; } // si se mueve el scroll se cambia el arma principal else if (event.button.button==SDL_BUTTON_WHEELUP) { player_change_weapon(&pj, WEAPON_PREV, 1); } else if (event.button.button==SDL_BUTTON_WHEELDOWN) { player_change_weapon(&pj, WEAPON_NEXT, 1); } // determinar si personaje fue herido if(mouse_x >= pj->x && mouse_x <= pj->x+SPRITE_SIZE && mouse_y >= pj->y && mouse_y <= pj->y+SPRITE_SIZE) { // hacer daño al personaje if(event.button.button==SDL_BUTTON_LEFT && pj->primary->fired) { pj->health -= pj->primary->damage; // reproducir sonido de dolor Mix_PlayChannel(-1, pj->sounds[PLAYER_SOUND_DAMAGED], 0); } else if(event.button.button==SDL_BUTTON_RIGHT && pj->secondary->fired) { pj->health -= pj->secondary->damage; // reproducir sonido de dolor Mix_PlayChannel(-1, pj->sounds[PLAYER_SOUND_DAMAGED], 0); } // si la salud quedo menor o igual a 0 el personaje muere if(pj->health<1) { // incrementar contador de muerte ++pj->dead; pj->health = conf->PLAYER_HEALTH; // descontar vida if(conf->PLAYER_LIFE!=0) { --pj->life; if(pj->life==0) { // GAME OVER } } // reposicionar personaje muerto player_set_position(&pj, map, -1, -1, mapobjects); } } } // si se suelta el mouse else if(event.type==SDL_MOUSEBUTTONUP) { // guardar coordenadas del mouse mouse_x = event.button.x; mouse_y = event.button.y; // si se solto el boton izquierdo if(event.button.button==SDL_BUTTON_LEFT) { } // si se solto el boton derecho else if(event.button.button==SDL_BUTTON_RIGHT) { } } } // mover jugador player_move(&pj, keystates, map, mapobjects, conf, SDL_GetTicks()-ticks); player_get_object(&pj, map, conf); // primera linea informacion sprintf(info1, "time: %02d:%02d, mouse: (%04d,%04d)", time/60, time%60, mouse_x, mouse_y); SDL_Surface *s_info1 = TTF_RenderText_Blended(fuente, info1, (SDL_Color) {255,255,255,255}); SDL_Rect rect = (SDL_Rect) { 10, 0, 100, 20 }; SDL_BlitSurface(s_info1, NULL, screen, &rect); SDL_FreeSurface(s_info1); // segunda linea informacion sprintf(info2, "[%s] +%03d, life: %02d, dead: %02d, weapon: [%d/%03d/%03d] [%d/%03d/%03d]", conf->PLAYER_NAME, pj->health, pj->life, pj->dead, pj->primary->id, pj->primary->damage, pj->primary->ammo, pj->secondary->id, pj->secondary->damage, pj->secondary->ammo); SDL_Surface *s_info2 = TTF_RenderText_Blended(fuente, info2, (SDL_Color) {255,255,255,255}); rect.y += 12; SDL_BlitSurface(s_info2, NULL, screen, &rect); SDL_FreeSurface(s_info2); // dibujar enemigos enemies_draw(screen, enemies); // dibujar bots /*for(i=0; i<conf->BOTS; ++i) { Player *aux = &bots[i]; SDL_BlitSurface(aux->images[PLAYER_IMAGE_SPRITE], &aux->src, screen, &aux->sprite); }*/ // dibujar jugador SDL_BlitSurface(pj->images[PLAYER_IMAGE_SPRITE], &pj->src, screen, &pj->sprite); // actualizar screen sdl SDL_Flip(screen); SDL_Delay(1000/FPS); // resetear ticks ticks = SDL_GetTicks(); } // cerrar conexion al servidor close(connection); // liberar jugador player_free(&pj); // liberar armas weapons_free(&weapons); // libear objetos del mapa mapobjects_free(&mapobjects); // cerrar fuentes TTF_CloseFont(fuente); TTF_Quit(); // cerrar SDL SDL_FreeSurface(screen); SDL_Quit(); // retorno del sistema return EXIT_SUCCESS; }