示例#1
0
void map_center(map_type *map)
{
	int					n,k;
	bool				first_hit;
	d3pnt				*pt,min,max,mov_pnt;
	map_mesh_type		*mesh;
	
		// get map size

	first_hit=TRUE;
	
	mesh=map->mesh.meshes;
		
	for (n=0;n!=map->mesh.nmesh;n++) {

		pt=mesh->vertexes;

		for (k=0;k!=mesh->nvertex;k++) {
		
			if (first_hit) {
				min.x=max.x=pt->x;
				min.y=max.y=pt->y;
				min.z=max.z=pt->z;
				first_hit=FALSE;
			}
			else {
				if (pt->x<min.x) min.x=pt->x;
				if (pt->x>max.x) max.x=pt->x;
				if (pt->y<min.y) min.y=pt->y;
				if (pt->y>max.y) max.y=pt->y;
				if (pt->z<min.z) min.z=pt->z;
				if (pt->z>max.z) max.z=pt->z;
			}

			pt++;
		}

		mesh++;
	}
	
	if (first_hit) return;

		// adjust map

	mov_pnt.x=(map_max_size/2)-((max.x+min.x)/2);
	mov_pnt.y=(map_max_size/2)-((max.y+min.y)/2);
	mov_pnt.z=(map_max_size/2)-((max.z+min.z)/2);

	map_move(map,&mov_pnt);
}
示例#2
0
static void maybe_move(ProxMapState* prox_map, Sensors* sens) {
    Position next;
    if (fabs(prox_map->lower_left.x + HALF_SIZE - sens->current.x) < 2 + FLICKER_INERTIA
        && fabs(prox_map->lower_left.y + HALF_SIZE - sens->current.y) < 1 + FLICKER_INERTIA) {
        return;
    }

    /* Need to move. */
    desired_position(&next, sens);
    /* If (for example) our offset increases, the map-data needs to shift
     * towards the negative.  Thus, we swap the signs here and let
     * map_move() believe that we are moving the map-data (and not our
     * frame of reference). */
    map_move(map_get_proximity(),
        next.x - prox_map->lower_left.x,
        next.y - prox_map->lower_left.y);

    prox_map->lower_left = next;
}
示例#3
0
/* TODO: comment this more. Sorry --lk */
void map_editor_input(state_stack* stack, SDL_Event *sdlEvent) {
	state_desc *top = (state_desc*) table_ind(stack, stack->m_len-1);
	map_editor *mapEditor = (map_editor*) top->m_pData;
	
	if (sdlEvent->type == SDL_QUIT) {
		// TODO: Check for unsaved changes
		state_stack_kill(stack);
	}
	
	// Handle keys which do the same thing no matter what the current state is.
	if (_editor_handle_generic_event(mapEditor, sdlEvent)) { return; }

	// We may as well always check for this, because why not?
	if (sdlEvent->type == SDL_MOUSEMOTION) {
		mapEditor->m_iMouseX = sdlEvent->motion.x;
		mapEditor->m_iMouseY = sdlEvent->motion.y;
	}

	switch (mapEditor->m_iMapEditorState) {
		case MAPEDITOR_EDIT:
			if  (sdlEvent->type == SDL_KEYDOWN) {
				// TODO: bounds checking.
				if (sdlEvent->key.keysym.sym == SDLK_LEFT) {
					mapEditor->m_iActiveTile--;
				}
				if (sdlEvent->key.keysym.sym == SDLK_RIGHT) {
					mapEditor->m_iActiveTile++;
				}
			}

			if (sdlEvent->type == SDL_MOUSEBUTTONDOWN) { // Drag map on right click & hold
				if (sdlEvent->button.button == 2 || sdlEvent->button.button == 3) {
					mapEditor->m_bDragMap = true;
				}
			}

			if (sdlEvent->type == SDL_MOUSEBUTTONUP) {
				if (sdlEvent->button.button == 1) { // place tile
					rect tempSrc = _tile_rect(mapEditor->m_iActiveTile);
					rect tempDst = {((mapEditor->m_iMouseX + g_map.m_rectView.x) / 32) * 32, ((mapEditor->m_iMouseY + g_map.m_rectView.y) / 32) * 32, 32, 32};
					g_map.m_map[(mapEditor->m_iMouseX + g_map.m_rectView.x) / 32][(mapEditor->m_iMouseY + g_map.m_rectView.y) / 32].m_iTileID = mapEditor->m_iActiveTile;
					image_draw_to(g_map.m_imageMap, g_map.m_imageTiles, &tempSrc, &tempDst);
					map_draw_view(); // Only redraw what's currently visible (unless the map maker is psychic the current view is what changed)
					if (mapEditor->m_bGrid == true) {
						map_draw_grid_view();
					}
				}
				if (sdlEvent->button.button == 2 || sdlEvent->button.button == 3) { // stop dragging map
					mapEditor->m_bDragMap = false;
				}
			}

			if (sdlEvent->type == SDL_MOUSEMOTION) {
				if (mapEditor->m_bDragMap == true) {
					map_move(sdlEvent->motion.xrel, sdlEvent->motion.yrel);
				}
			}
			break;

		/* Rename map. */
		case MAPEDITOR_NAME:
			if (util_textInput(sdlEvent, &g_map.m_cName) == 1) {
				mapEditor->m_iMapEditorState = MAPEDITOR_EDIT;
			}
			break;

		/* Load map. */
		case MAPEDITOR_LOAD:
			if (util_textInput(sdlEvent, &g_map.m_cName) == 1) {
				map_load(g_map.m_cName);
				map_draw();
				mapEditor->m_iMapEditorState = MAPEDITOR_EDIT;
			}
			break;

		case MAPEDITOR_TILE:
			// Draw tilesheet
			// Input based on tile sheet, not map.
			// Choose tile on click
			// g_iMapEditorState = MAPEDITOR_EDIT
			break;

		/* collision editor. */
		case MAPEDITOR_WALK:
			if  (sdlEvent->type == SDL_KEYDOWN) {
				if (sdlEvent->key.keysym.sym == SDLK_LEFT) {
					if (mapEditor->m_cMapWalk > 0) {
						mapEditor->m_cMapWalk--;
					}
				}
				if (sdlEvent->key.keysym.sym == SDLK_RIGHT) {
					if (mapEditor->m_cMapWalk < WALK_MAX) {
						mapEditor->m_cMapWalk++;
					}
				}
			}

			if (sdlEvent->type == SDL_MOUSEBUTTONDOWN) { // Drag map on right click & hold
				if (sdlEvent->button.button == 2 || sdlEvent->button.button == 3) {
					mapEditor->m_bDragMap = true;
				}
			}
			if (sdlEvent->type == SDL_MOUSEBUTTONUP) {
				if (sdlEvent->button.button == 1) { // place walk
					g_map.m_map[(mapEditor->m_iMouseX + g_map.m_rectView.x) / 32][(mapEditor->m_iMouseY + g_map.m_rectView.y) / 32].m_cWalk = mapEditor->m_cMapWalk;
					map_draw_view(); // Only redraw what's currently visible
					map_editor_draw_walk_view();
					if (mapEditor->m_bGrid == true) {
						map_draw_grid_view();
					}
				}
				if (sdlEvent->button.button == 2 || sdlEvent->button.button == 3) { // stop dragging map
					mapEditor->m_bDragMap = false;
				}
			}

			if (sdlEvent->type == SDL_MOUSEMOTION) {
				if (mapEditor->m_bDragMap == true) {
					map_move(sdlEvent->motion.xrel, sdlEvent->motion.yrel);
				}
			}
			break;
		default:
			break;
	}

	return;
}
示例#4
0
文件: worker.c 项目: lytsing/ytht
int
main(int n, char *cmd[])
{
	int c, m, i;
	int dx, dy;

	telnet_init();
	if (map_init() == 0)
		printf("map.dat error\n");
	if (n >= 2) {
		char buf[40];
		strsncpy(userid, cmd[1], sizeof (userid));
		readuservalue(userid, "worker.laststage", buf, sizeof (buf));
		lastMaxStage = atoi(buf);
		if (lastMaxStage >= map_total)
			lastMaxStage = map_total - 1;
		stage = lastMaxStage;
	}
	if (n >= 3)
		strsncpy(fromhost, cmd[2], sizeof (fromhost));

	if (stage == 0) {
		clear();
		printf("»¶Ó­¹âÁÙÍÆÏä×ÓÓÎÏ·¡£\r\n");
		printf
		    ("¹æÔòºÜ¼òµ¥£¬Ö»Ðè°ÑËùÓеÄ'¡õ'¶¼ÍƵ½'¡¤'ÉÏÃæÈ¥(»á±ä³ÉÂÌÉ«)¾Í¹ý¹ØÁË¡£\r\n");
		printf("µ«ÍæÆðÀ´ÄѶȿÉÊÇÏ൱´óµÄ£¬²»ÒªÇáÊÓม£\r\n");
		pressanykey();
		goto start;
	}

	win_showrec();
	clear();

	printf("ÇëÓ÷½Ïò¼üÑ¡¹Ø, »Ø³µ¼üÈ·ÈÏ: %d ", stage);
	move(0, 14);
	while (1) {
		c = inkey();
		if ((c == KEY_LEFT || c == KEY_UP) && stage > 0)
			stage--;
		if ((c == KEY_RIGHT || c == KEY_DOWN) && stage < lastMaxStage)
			stage++;
		if (c == 10 || c == 13)
			break;
		if (c == 3 || c == 4 || c == 32)
			quit();
		move(0, 14);
		printf("%d  ", stage);
		move(0, 14);
	}

      start:
	if (stage < 0 || stage >= map_total)
		stage = 0;
	clear();
	printf("ÍÆÏä×Ó: µÚ %d ¹Ø:\033[m", stage);
	move(20, 0);
	refresh();
	sleep(1);
      start2:
	for (n = 0; n < 20; n++)
		for (m = 0; m < 30; m++)
			map_now[n][m] = map_data[stage][n][m];
	if (!find_y_x(&now_y, &now_x))
		printf("stage error\n");
	map_show();
	bzero(&my_history, sizeof (my_history));
	while (1) {
		c = inkey();
		if (my_history.max >= 1999) {
			move(21, 0);
			printf("ÄãÓÃÁË2000²½»¹Ã»Óйý¹Ø! GAME OVER.");
			quit();
		}
		dx = 0;
		dy = 0;
		if (c == 8 && my_history.max > 0) {
			my_history.max--;
			i = my_history.max;
			map_move(my_history.y1[i], my_history.x1[i],
				 my_history.y0[i], my_history.x0[i]);
			find_y_x(&now_y, &now_x);
			move(now_y, now_x);
			continue;
		}

		if (c == ' ')
			quit();
		if (c == '')
			map_show();
		if (c == 9)
			goto start2;

		if (c == KEY_UP)
			dy = -1;
		if (c == KEY_DOWN)
			dy = 1;
		if (c == KEY_LEFT)
			dx = -1;
		if (c == KEY_RIGHT)
			dx = 1;

		if (dx == 0 && dy == 0)
			continue;

		if (map_now[now_y + dy][now_x + dx] & 4)
			if (map_now[now_y + dy * 2][now_x + dx * 2] < 2) {
				map_move(now_y + dy, now_x + dx, now_y + dy * 2,
					 now_x + dx * 2);
				i = my_history.max;
				my_history.y0[i] = now_y + dy;
				my_history.x0[i] = now_x + dx;
				my_history.y1[i] = now_y + dy * 2;
				my_history.x1[i] = now_x + dx * 2;
				my_history.max++;
			}
		if (map_now[now_y + dy][now_x + dx] < 2) {
			map_move(now_y, now_x, now_y + dy, now_x + dx);
			i = my_history.max;
			my_history.y0[i] = now_y;
			my_history.x0[i] = now_x;
			my_history.y1[i] = now_y + dy;
			my_history.x1[i] = now_x + dx;
			my_history.max++;
		}
		if (check_if_win())
			break;
		find_y_x(&now_y, &now_x);
		move(now_y, now_x);
	}
	move(19, 0);
	printf("×£ºØÄã, Äã³É¹¦ÁË£¡");
	steps = my_history.max;
	win_checkrec();

	stage++;
	if (stage > lastMaxStage) {
		lastMaxStage = stage;
		if (strcmp(userid, "null.")) {
			char buf[30];
			sprintf(buf, "%d", lastMaxStage);
			saveuservalue(userid, "worker.laststage", buf);
		}
	}
	goto start;
}
示例#5
0
文件: tilemap.c 项目: MKelm/sdlmix
int main(int argc, char* args[]) {
  if (init() == FALSE) {
    return 1;
  }
  if (load_files() == FALSE) {
    return 1;
  }

  message = TTF_RenderText_Solid(font, window_footer_str, font_color);
  if (message == NULL) {
    return 1;
  }

  Uint32 frameStart = 0;
  int rightMouseButtonDown = FALSE;
  int quit = FALSE;
  while (quit == FALSE) {
    frameStart = SDL_GetTicks();

    while (SDL_PollEvent(&event)) {
      if (event.type == SDL_MOUSEBUTTONDOWN) {
        if (event.button.button == SDL_BUTTON_RIGHT)
          rightMouseButtonDown = TRUE;
        else if (event.button.button == SDL_BUTTON_LEFT)
          map_select_tile(event.button.x, event.button.y);
      }
      if (event.type == SDL_MOUSEBUTTONUP) {
        if (event.button.button == SDL_BUTTON_RIGHT) {
          rightMouseButtonDown = FALSE;
          map_move_reset();
        }
      }
      if (event.type == SDL_MOUSEMOTION) {
        if (rightMouseButtonDown == TRUE) {
          map_move(event.button.x, event.button.y);
        }
      }
      if (event.type == SDL_KEYDOWN) {
        switch (event.key.keysym.sym) {
          case SDLK_g: map_toggle_grid(); break;
          case SDLK_PLUS: map_resize(1); break;
          case SDLK_MINUS: map_resize(-1); break;
          case SDLK_l: map_load(); break;
          default: ;
        }
      }
      if (event.type == SDL_QUIT) {
        quit = TRUE;
      }
    }

    SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 0, 0, 0));
    map_show();
    apply_surface(
      screen_width - message->w, screen_height - message->h, message, screen
    );

    if (SDL_Flip(screen) == -1) {
      return 1;
    }

    if ((SDL_GetTicks() - frameStart) < (1000 / FPS)) {
      SDL_Delay((1000 / FPS) - (SDL_GetTicks() - frameStart));
    }
  }

  clean_up();
  return 0;
}
示例#6
0
文件: rockpush.c 项目: amfo/rockpush
int8_t rockpush_menu(Rock_Screen *screen_data)
{
  SDL_Event event;
  SDL_Surface *menu;
  SDL_Surface *sprite_textures;
  SDL_Texture *font_surface;
  Rock_Sprite *rocco;
  Rock_Scroll_Map map;
  Mix_Music *music;
  TTF_Font *menu_font;
  bool done = false;
  Uint32 init_ms;
  char font_path[128];
  int16_t offset;
  int8_t orientation, total_options, option;
  Sint32 event_option;
  SDL_Joystick *joystick = NULL;
  Sint16 joy_event = 0, joyy = 0;

    if (SDL_NumJoysticks() >= 1) {
        joystick = SDL_JoystickOpen(0);
        SDL_JoystickEventState(SDL_ENABLE);
    }

    if ((menu_font = TTF_OpenFont(MENU_TTF, 34)) == NULL) {
        printf("Error %s\n", TTF_GetError());
        exit(EXIT_FAILURE);
    }

    gfx_check_gpu(screen_data->screen);

    if (gfx_get_low_gpu())
        sprintf(font_path, "%s", TINY_FONT_PATH);
    else
        sprintf(font_path, "%s", FONT_PATH);

    if ((font_surface = IMG_LoadTexture(screen_data->screen, font_path)) == NULL) {
        printf("La fuente bitmap no encontrada %s\n", SDL_GetError());
        exit(EXIT_FAILURE);
    }

    menu            = screen_make_surface(SCREEN_WIDTH, SCREEN_HEIGHT);
    sprite_textures = screen_make_surface(screen_data->sprites_surface->w, screen_data->sprites_surface->h);

    map.view_height   = MINI_HEIGHT * TILE_SIZE;
    map.view_width    = MINI_WIDTH  * TILE_SIZE;
    map.scroll_shift  = SCROLL_SHIFT / 2;
    map.diamonds      = 0;
    map.level         = (rand() % 15);
    map.points        = 0;
    map.lives         = INIT_ROCCO_LIVE;
    map.update_score  = false;
    map.refresh_rocco = false;

    rocco = set_level_rocco(&map);
    rocco->active = false;
    map.objects_map[rocco->x_map][rocco->y_map] = EMPTY;
    map.rockmap[rocco->x_map][rocco->y_map]     = GRASS;
    sprites_set_tiles_textures();

    //Se hace una copia de las texturas cargadas para convertirlas a B/N
    SDL_BlitSurface(screen_data->sprites_surface, NULL, sprite_textures, NULL);
    screen_greyscale(screen_data->sprites_surface);

    orientation = (rand() % 4) + 2;
    option = offset = 0;
    total_options = menu_options(option, map.view_width, menu, menu_font);
    gfx_init();

    screen_data->blit_surface = true;

    SDL_SetRenderDrawColor(screen_data->screen, 0, 0, 0, 255);
    SDL_RenderClear(screen_data->screen);

    if (sfx_get_active()) {
        music = Mix_LoadMUS(MENU_MUSIC);
        Mix_PlayMusic(music, -1);
    }

    while (!done) {

        init_ms = SDL_GetTicks();

        map_show(screen_data, &map, false);
        map_move(&map, orientation);
        SDL_BlitSurface(menu, NULL, screen_data->buffer_surface, NULL);
        //sprites_update(&map);

        screen_dump_buffer(screen_data, map.view_width, map.view_height);
        offset ++;

        if (offset > 240) {
            orientation = (rand() % 4) + 2;
            offset = 0;
        }


        /* Desplazamiento de texto */
        gfx_text_move(screen_data->screen, font_surface);

        while (SDL_PollEvent(&event)) {

            if (event.type == SDL_QUIT) {
                option = 0;
                done   = true;
            }

            if (SDL_JoystickGetAttached(joystick))
                joy_event = SDL_JoystickGetAxis(joystick, 0) | SDL_JoystickGetAxis(joystick, 1);

            if (event.type == SDL_KEYDOWN || event.type == SDL_JOYBUTTONDOWN || joy_event) {

                event_option = event.key.keysym.sym;

                if (SDL_JoystickGetAttached(joystick)) {
                    joyy      = SDL_JoystickGetAxis(joystick, 1);
                    joy_event = SDL_JoystickGetAxis(joystick, 0) | joyy;

                    if (SDL_JoystickGetButton(joystick, 0))
                        event_option = SDLK_ESCAPE;

                    if (SDL_JoystickGetButton(joystick, 1))
                        event_option = SDLK_RETURN;

                    if (joyy < -10)
                        event_option = SDLK_UP;

                    if (joyy > 10)
                        event_option = SDLK_DOWN;
                }

                switch (event_option) {
                    case SDLK_UP:
                        if (option > 0)
                            option --;

                        break;

                    case SDLK_DOWN:
                        if (option < total_options)
                            option ++;
                        break;

                    case SDLK_ESCAPE:
                        option = -1;
                    case SDLK_RETURN:
                        done = true;
                        option ++;
                        break;
                }

                menu_options(option, map.view_width, menu, menu_font);
            }
        }

        while ((SDL_GetTicks() - init_ms) < TICK_RATE);

    }

    //Restauramos la copia
    SDL_BlitSurface(sprite_textures, NULL, screen_data->sprites_surface, NULL);

    SDL_SetRenderDrawColor(screen_data->screen, 0, 0, 0, 255);
    SDL_RenderClear(screen_data->screen);
    screen_data->blit_surface = false;

    SDL_FreeSurface(sprite_textures);
    SDL_DestroyTexture(font_surface);
    SDL_FreeSurface(menu);
    TTF_CloseFont(menu_font);

    if (SDL_JoystickGetAttached(joystick))
        SDL_JoystickClose(joystick);

    if (sfx_get_active()) {
        Mix_FadeOutMusic(SFX_FADE_OUT);
        while (Mix_PlayingMusic()) ;
        Mix_FreeMusic(music);
    }

    return option;
}