Ignore:
Timestamp:
May 15, 2011, 6:22:17 AM (11 years ago)
Author:
Sam Hocevar
Message:

game: refactor the mouse/game coordinate conversion logic.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • abuse/trunk/src/dev.cpp

    r656 r657  
    491491    if (link_object)
    492492    {
    493       int32_t rx1,ry1;
    494       the_game->game_to_mouse(link_object->x,link_object->y,v,rx1,ry1);
    495       main_screen->Line(vec2i(rx1, ry1), vec2i(dlastx, dlasty), yellow);
     493      vec2i pos = the_game->GameToMouse(vec2i(link_object->x, link_object->y), v);
     494      main_screen->Line(pos, vec2i(dlastx, dlasty), yellow);
    496495    }
    497496
    498497    if (selected_light)
    499498    {
    500       image *i=cache.img(light_buttons[0]);
    501       int l=i->Size().x/2,h=i->Size().y/2;
    502       int32_t rx1,ry1;
    503       the_game->game_to_mouse(selected_light->x,selected_light->y,v,rx1,ry1);
    504       main_screen->Rectangle(vec2i(rx1 - l, ry1 - h), vec2i(rx1 + l, ry1 + h),
     499      image *im = cache.img(light_buttons[0]);
     500      vec2i pos = the_game->GameToMouse(vec2i(selected_light->x, selected_light->y), v);
     501      main_screen->Rectangle(pos - im->Size() / 2, pos + im->Size() / 2,
    505502                             wm->bright_color());
    506503    }
     
    510507      for (o=current_level->first_object(); o; o=o->next)
    511508      {
    512     the_game->game_to_mouse(o->x,o->y,current_view,x1,y1);
     509    vec2i pos = the_game->GameToMouse(vec2i(o->x, o->y), current_view);
    513510    char *nm=object_names[o->otype];
    514     console_font->put_string(main_screen,x1-strlen(nm)*console_font->width()/2,y1+2,nm);
     511    console_font->put_string(main_screen, pos.x - strlen(nm) * console_font->width() / 2, pos.y + 2, nm);
    515512      }
    516513
     
    520517      for (o=current_level->first_object(); o; o=o->next)
    521518      {
    522     the_game->game_to_mouse(o->x,o->y,current_view,x1,y1);
     519    vec2i pos1 = the_game->GameToMouse(vec2i(o->x, o->y), current_view);
    523520
    524521    int i=0;
     
    526523    {
    527524      game_object *other=o->get_object(i);
    528       the_game->game_to_mouse(other->x,other->y,current_view,x2,y2);
    529       main_screen->Line(vec2i(x1, y1), vec2i(x2, y2), wm->bright_color());
     525      vec2i pos2 = the_game->GameToMouse(vec2i(other->x, other->y), current_view);
     526      main_screen->Line(pos1, pos2, wm->bright_color());
    530527    }
    531528
     
    533530    {
    534531      light_source *l=o->get_light(i);
    535       the_game->game_to_mouse(l->x,l->y,current_view,x2,y2);
    536       main_screen->Line(vec2i(x1, y1), vec2i(x2, y2), light_connection_color);
     532      vec2i pos2 = the_game->GameToMouse(vec2i(l->x, l->y), current_view);
     533      main_screen->Line(pos1, pos2, light_connection_color);
    537534    }
    538535
     
    543540    {
    544541      selected_object->picture_space(x1,y1,x2,y2);
    545       int32_t rx1,ry1,rx2,ry2;
    546       the_game->game_to_mouse(x1,y1,v,rx1,ry1);
    547       the_game->game_to_mouse(x2,y2,v,rx2,ry2);
    548       main_screen->Rectangle(vec2i(rx1, ry1), vec2i(rx2, ry2),
    549                              wm->bright_color());
    550 
    551       the_game->game_to_mouse(selected_object->x,selected_object->y,current_view,x1,y1);
     542      vec2i pos1 = the_game->GameToMouse(vec2i(x1, y1), v);
     543      vec2i pos2 = the_game->GameToMouse(vec2i(x2, y2), v);
     544      main_screen->Rectangle(pos1, pos2, wm->bright_color());
     545
     546      pos1 = the_game->GameToMouse(vec2i(selected_object->x, selected_object->y), current_view);
    552547      for (int i=0; i<selected_object->total_objects(); i++)
    553548      {
    554     game_object *other=selected_object->get_object(i);
    555     the_game->game_to_mouse(other->x,other->y,current_view,x2,y2);
    556     main_screen->Line(vec2i(x1, y1), vec2i(x2, y2), light_connection_color);
    557       }
    558     }
    559 
    560 
     549        game_object *other = selected_object->get_object(i);
     550        pos2 = the_game->GameToMouse(vec2i(other->x, other->y), current_view);
     551        main_screen->Line(pos1, pos2, light_connection_color);
     552      }
     553    }
    561554  }
    562555}
     
    10531046  if (!strcmp(fword,"unchop"))
    10541047  {
    1055     int32_t rx,ry;
    1056     the_game->btile_on(dlastx,dlasty,rx,ry);
    1057     if (rx>=0 && ry>=0)
     1048    vec2i tile = the_game->GetBgTile(vec2i(dlastx, dlasty));
     1049    if (tile.x>=0 && tile.y>=0)
    10581050    {
    10591051      if (sscanf(command,"%s%d%d",fword,&l,&h)==3)
     
    10641056    for (y=0,i=cur_bg; y<h; y++)
    10651057          for (x=0; x<l; x++)
    1066             the_game->put_bg(rx+x,ry+y,i++);
     1058            the_game->put_bg(tile.x+x,tile.y+y,i++);
    10671059    dprintf("%dx%d\n",l,h);
    10681060      } else dprintf(symbol_str("unchop1"));
     
    11751167    if (t>=0)                                 // did we find it?
    11761168    {
    1177       int32_t rx,ry;
    1178       the_game->mouse_to_game(dlastx,dlasty,rx,ry);
    1179       edit_object=create(t,rx,ry);
     1169      vec2i pos = the_game->MouseToGame(vec2i(dlastx, dlasty));
     1170      edit_object=create(t, pos.x, pos.y);
    11801171      current_level->add_object(edit_object);
    11811172      the_game->need_refresh();
     
    12201211  if (!strcmp(fword,"fg_select"))
    12211212  {
    1222     int32_t x,y;
    1223     the_game->ftile_on(dlastx,dlasty,x,y);
    1224     if (x>=0 && y>=0 && x<current_level->foreground_width() &&
    1225     y<current_level->foreground_height())
    1226     {
    1227       cur_fg=current_level->get_fg(x,y);
     1213    vec2i tile = the_game->GetFgTile(vec2i(dlastx, dlasty));
     1214    if (tile.x >= 0 && tile.y >= 0 &&
     1215        tile.x < current_level->foreground_width() &&
     1216        tile.y < current_level->foreground_height())
     1217    {
     1218      cur_fg=current_level->get_fg(tile.x, tile.y);
    12281219      if (forew)
    12291220    ((tile_picker *)forew->read(DEV_FG_PICKER))->recenter(forew->m_surf);
     
    12341225  if (!strcmp(fword,"toggle_fg_raise"))
    12351226  {
    1236     int32_t x,y;
    1237     the_game->ftile_on(dlastx,dlasty,x,y);
    1238     if (x>=0 && y>=0 && x<current_level->foreground_width() &&
    1239     y<current_level->foreground_height())
    1240       current_level->fg_set_raised(x,y,!current_level->fg_raised(x,y));
     1227    vec2i tile = the_game->GetFgTile(vec2i(dlastx, dlasty));
     1228    if (tile.x >= 0 && tile.y >= 0 &&
     1229        tile.x < current_level->foreground_width() &&
     1230        tile.y < current_level->foreground_height())
     1231      current_level->fg_set_raised(tile.x, tile.y,!current_level->fg_raised(tile.x,tile.y));
    12411232  }
    12421233
     
    15231514  if (ev.type==EV_MOUSE_BUTTON && ev.mouse_button)
    15241515  {
    1525     int32_t gx,gy;
    1526     the_game->mouse_to_game(last_demo_mx,last_demo_my,gx,gy);
     1516    vec2i pos = the_game->MouseToGame(vec2i(last_demo_mx, last_demo_my));
    15271517    if (!current_level) return ;
    1528     current_area=current_level->area_list=new area_controller(gx,gy,
     1518    current_area=current_level->area_list=new area_controller(pos.x, pos.y,
    15291519                                  the_game->ftile_width(),
    15301520                                  the_game->ftile_height(),
     
    15641554  {
    15651555    int32_t mx=last_demo_mx,my=last_demo_my;
    1566     view *v=the_game->view_in(mx,my);
     1556    view *v = the_game->GetView(vec2i(mx, my));
    15671557    for (area_controller *a=current_level->area_list; a; a=a->next)
    15681558    {
    1569       int32_t x1,y1,x2,y2;
    1570       the_game->game_to_mouse(a->x,a->y,v,x1,y1);
    1571       the_game->game_to_mouse(a->x+a->w,a->y+a->h,v,x2,y2);
    1572       if (abs(x1-mx)<2 && abs(y1-my)<2)
    1573       { find=a;    find_top=1; }
    1574       else if (abs(x2-mx)<2 && abs(y2-my)<2)
    1575       { find=a;    find_top=0; }
     1559      vec2i pos1 = the_game->GameToMouse(vec2i(a->x, a->y), v);
     1560      vec2i pos2 = the_game->GameToMouse(vec2i(a->x + a->w, a->y + a->h), v);
     1561      if (abs(pos1.x - mx) < 2 && abs(pos1.y - my) < 2)
     1562      { find = a; find_top = 1; }
     1563      else if (abs(pos2.x - mx) < 2 && abs(pos2.y - my) < 2)
     1564      { find = a; find_top = 0; }
    15761565    }
    15771566
     
    16971686    if (ev.type==EV_MOUSE_MOVE)
    16981687    {
    1699       the_game->mouse_to_game(last_demo_mx,last_demo_my,edit_object->x,edit_object->y);
    1700       edit_object->x=snap_x(edit_object->x);
    1701       edit_object->y=snap_y(edit_object->y);
     1688      vec2i pos = the_game->MouseToGame(vec2i(last_demo_mx, last_demo_my));
     1689      edit_object->x = snap_x(pos.x);
     1690      edit_object->y = snap_y(pos.y);
    17021691      the_game->need_refresh();
    17031692    }
     
    17251714    if (ev.type==EV_MOUSE_MOVE)
    17261715    {
    1727       the_game->mouse_to_game(last_demo_mx,last_demo_my,edit_light->x,edit_light->y);
    1728       edit_light->x=snap_x(edit_light->x);
    1729       edit_light->y=snap_y(edit_light->y);
     1716      vec2i pos = the_game->MouseToGame(vec2i(last_demo_mx, last_demo_my));
     1717      edit_light->x = snap_x(pos.x);
     1718      edit_light->y = snap_y(pos.y);
    17301719
    17311720      edit_light->calc_range();
     
    18071796      if (current_area)
    18081797      {
    1809     int32_t gx,gy;
    1810     the_game->mouse_to_game(last_demo_mx,last_demo_my,gx,gy);
    1811     if (gx>current_area->x && gy>current_area->y)
    1812     {
    1813       if (gx-current_area->x!=current_area->w || gy-current_area->y!=current_area->h)
     1798    vec2i pos = the_game->MouseToGame(vec2i(last_demo_mx, last_demo_my));
     1799    if (pos.x>current_area->x && pos.y>current_area->y)
     1800    {
     1801      if (pos.x-current_area->x!=current_area->w || pos.y-current_area->y!=current_area->h)
    18141802      {
    18151803        the_game->need_refresh();
    1816         current_area->w=gx-current_area->x;
    1817         current_area->h=gy-current_area->y;
     1804        current_area->w=pos.x-current_area->x;
     1805        current_area->h=pos.y-current_area->y;
    18181806      }
    18191807    }
     
    18301818      if (current_area)
    18311819      {
    1832     int32_t gx,gy;
    1833     the_game->mouse_to_game(last_demo_mx,last_demo_my,gx,gy);
    1834     if (gx<current_area->x+current_area->w && gy<current_area->y+current_area->h)
    1835     {
    1836       if (gx!=current_area->x || gy!=current_area->y)
     1820    vec2i pos = the_game->MouseToGame(vec2i(last_demo_mx, last_demo_my));
     1821    if (pos.x<current_area->x+current_area->w && pos.y<current_area->y+current_area->h)
     1822    {
     1823      if (pos.x!=current_area->x || pos.y!=current_area->y)
    18371824      {
    18381825        the_game->need_refresh();
    1839         current_area->x=gx;
    1840         current_area->y=gy;
     1826        current_area->x=pos.x;
     1827        current_area->y=pos.y;
    18411828      }
    18421829    }
     
    18571844    if (ev.window==NULL)
    18581845    {
    1859       int32_t rx,ry;
    1860       the_game->mouse_to_game(last_demo_mx,last_demo_my,rx,ry);
     1846      vec2i pos = the_game->MouseToGame(vec2i(last_demo_mx, last_demo_my));
    18611847
    18621848      if (!(dev & MAP_MODE))
    18631849      {
    18641850        if (dev&DRAW_PEOPLE_LAYER)
    1865               selected_object=current_level->find_object(rx,ry);
     1851              selected_object=current_level->find_object(pos.x, pos.y);
    18661852        light_source *old_light=selected_light;
    1867         if (selected_object)
    1868           selected_light=NULL;
    1869         else
    1870           selected_light=find_light(rx,ry);
     1853        selected_light = selected_object ? NULL : find_light(pos.x, pos.y);
    18711854        if (selected_light!=old_light)
    18721855          the_game->need_refresh();
     
    18751858      if (edit_mode==ID_DMODE_DRAW)
    18761859      {
     1860        // FIXME: there is a bug here, the two if conditionals are the same
    18771861        if (ev.mouse_button==1 && !selected_object && !selected_light)
    18781862        {
    1879           int32_t xs,ys;
    1880           the_game->ftile_on(last_demo_mx,last_demo_my,xs,ys);
    1881           if (xs>=0 && ys>=0 && xs<current_level->foreground_width() &&
    1882           ys<current_level->foreground_height())
    1883           current_level->put_fg(xs,ys,raise_all ? make_above_tile(cur_fg) : cur_fg);
     1863          vec2i tile = the_game->GetFgTile(vec2i(last_demo_mx, last_demo_my));
     1864          if (tile.x>=0 && tile.y>=0 && tile.x<current_level->foreground_width() &&
     1865          tile.y<current_level->foreground_height())
     1866          current_level->put_fg(tile.x,tile.y,raise_all ? make_above_tile(cur_fg) : cur_fg);
    18841867          the_game->need_refresh();
    18851868        } else if (ev.mouse_button==1 && !selected_object && !selected_light)
    18861869        {
    1887           int32_t xs,ys;
    1888           the_game->btile_on(last_demo_mx,last_demo_my,xs,ys);
    1889           if (xs>=0 && ys>=0 && xs<current_level->background_width() &&
    1890           ys<current_level->background_height())
    1891           current_level->put_bg(xs,ys,cur_fg);
     1870          vec2i tile = the_game->GetBgTile(vec2i(last_demo_mx, last_demo_my));
     1871          if (tile.x>=0 && tile.y>=0 && tile.x<current_level->background_width() &&
     1872          tile.y<current_level->background_height())
     1873          current_level->put_bg(tile.x,tile.y,cur_fg);
    18921874          the_game->need_refresh();
    18931875        }
     
    19711953          if ((dev & DRAW_FG_LAYER) && ev.mouse_button==1)
    19721954          {
    1973         the_game->ftile_on(last_demo_mx,last_demo_my,x,y);
    1974         if (x>=0 && y>=0 && x<current_level->foreground_width() &&
    1975             y<current_level->foreground_height())
    1976         the_game->put_fg(x,y,raise_all ? make_above_tile(cur_fg) : cur_fg);
     1955        vec2i tile = the_game->GetFgTile(vec2i(last_demo_mx, last_demo_my));
     1956        if (tile.x>=0 && tile.y>=0 && tile.x<current_level->foreground_width() &&
     1957            tile.y<current_level->foreground_height())
     1958        the_game->put_fg(tile.x,tile.y,raise_all ? make_above_tile(cur_fg) : cur_fg);
    19771959          }
    19781960          if ((dev & DRAW_BG_LAYER) && ev.mouse_button==2)
    19791961          {
    1980         the_game->btile_on(last_demo_mx,last_demo_my,x,y);
    1981         if (x>=0 && y>=0 && x<current_level->background_width() &&
    1982             y<current_level->background_height())
    1983         the_game->put_bg(x,y,cur_bg);
     1962        vec2i tile = the_game->GetBgTile(vec2i(last_demo_mx, last_demo_my));
     1963        if (tile.x>=0 && tile.y>=0 && tile.x<current_level->background_width() &&
     1964            tile.y<current_level->background_height())
     1965        the_game->put_bg(tile.x,tile.y,cur_bg);
    19841966          }
    19851967        }
     
    25032485    case DEV_LIGHT9 :
    25042486    {
    2505       int32_t lx,ly;
    2506       the_game->mouse_to_game(last_demo_mx,last_demo_my,lx,ly);
    2507       lx=snap_x(lx);
    2508       ly=snap_y(ly);
    2509       edit_light=add_light_source(ev.message.id-DEV_LIGHT0,lx,ly,
    2510                        atoi(lightw->read(DEV_LIGHTR1)),
    2511                        atoi(lightw->read(DEV_LIGHTR2)),
    2512                        atoi(lightw->read(DEV_LIGHTW)),
    2513                        atoi(lightw->read(DEV_LIGHTH)));
     2487      vec2i pos = the_game->MouseToGame(vec2i(last_demo_mx, last_demo_my));
     2488      edit_light = add_light_source(ev.message.id - DEV_LIGHT0,
     2489                                    snap_x(pos.x), snap_y(pos.y),
     2490                                    atoi(lightw->read(DEV_LIGHTR1)),
     2491                                    atoi(lightw->read(DEV_LIGHTR2)),
     2492                                    atoi(lightw->read(DEV_LIGHTW)),
     2493                                    atoi(lightw->read(DEV_LIGHTH)));
    25142494      state=DEV_MOVE_LIGHT;
    25152495    } break;
     
    27522732        if (ev.window==NULL || ev.window==forew)
    27532733        {
    2754           the_game->ftile_on(last_demo_mx,last_demo_my,x,y);
    2755           fg_fill(cur_fg,x,y,NULL);
     2734          vec2i tile = the_game->GetFgTile(vec2i(last_demo_mx, last_demo_my));
     2735          fg_fill(cur_fg, tile.x, tile.y, NULL);
    27562736        }
    27572737      } break;
     
    28382818      case 'w' :
    28392819      {
    2840         int32_t rx,ry;
    2841         the_game->mouse_to_game(dlastx,dlasty,rx,ry);
    2842         char msg[100]; sprintf(msg,symbol_str("mouse_at"),rx,ry);
     2820        vec2i pos = the_game->MouseToGame(vec2i(dlastx, dlasty));
     2821        char msg[100]; sprintf(msg, symbol_str("mouse_at"), pos.x, pos.y);
    28432822        the_game->show_help(msg);
    28442823        the_game->need_refresh();
     
    28602839        if (current_level && player_list && player_list->focus)
    28612840        {
    2862           int32_t rx,ry;
    2863           the_game->mouse_to_game(dlastx,dlasty,rx,ry);
    2864           player_list->focus->x=rx;
    2865           player_list->focus->y=ry;
     2841          vec2i pos = the_game->MouseToGame(vec2i(dlastx, dlasty));
     2842          player_list->focus->x = pos.x;
     2843          player_list->focus->y = pos.y;
    28662844          do_command("center",ev);
    28672845          the_game->need_refresh();
     
    28702848      case 'z' : do_command("clear_weapons",ev); break;
    28712849      case 'Z' : if (dev&EDIT_MODE)
    2872       { view *v=the_game->view_in(last_demo_mx,last_demo_my);
     2850      { view *v = the_game->GetView(vec2i(last_demo_mx, last_demo_my));
    28732851        if (v)
    28742852        {
     
    31243102      case ' ' :
    31253103      {
    3126         int32_t xs,ys,xx,yy;
    3127         the_game->ftile_on(me->x,me->y,xs,ys);
    3128 
    3129         for (xx=xs; xx<xs+w; xx++)
     3104        int32_t xx, yy;
     3105        vec2i tile = the_game->GetFgTile(vec2i(me->x, me->y));
     3106
     3107        for (xx=tile.x; xx<tile.x+w; xx++)
    31303108        {
    3131           for (yy=ys; yy<ys+h; yy++)
     3109          for (yy=tile.y; yy<tile.y+h; yy++)
    31323110          {
    31333111        if (xx>=0 && yy>=0 && xx<current_level->foreground_width() &&
    31343112            yy<current_level->foreground_height())
    3135           the_game->put_fg(xx,yy,raise_all ? make_above_tile(pat[xx-xs+(yy-ys)*w]) : pat[xx-xs+(yy-ys)*w] );
     3113          the_game->put_fg(xx,yy,raise_all ? make_above_tile(pat[xx-tile.x+(yy-tile.y)*w]) : pat[xx-tile.x+(yy-tile.y)*w] );
    31363114          }
    31373115        }
     
    31393117      case 't' :
    31403118      {
    3141         int32_t xs,ys;
    3142         the_game->ftile_on(me->x,me->y,xs,ys);
    3143         dev_cont->fg_fill(-1,xs,ys,this);
     3119        vec2i tile = the_game->GetFgTile(vec2i(me->x, me->y));
     3120        dev_cont->fg_fill(-1, tile.x, tile.y, this);
    31443121      } break;
    31453122
Note: See TracChangeset for help on using the changeset viewer.