jak zrobić skrolowanie mapy
mapa to tablica 100x100 pól
type pole=record
id:integer;
im:byte
end;
map:array[1..100,1..100] of pole;
im to image z imagelist bitmapek o rozmiarach 32x32
zgrałem przykład z neta ale tam jest pod kontem to zrobione i nie wiem co zmienić żeby było normalnie
to kodzik tego co zgrałem:
unit Main;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, { Dialogs,}
DXClass, ExtCtrls, DXDraws, StdCtrls, DIB;
const
isoheight = 64;
isowidth = 128;
maxmapwidth = 128;
maxmapheight = 256;
safeedge = 512; // don't go past safe edge
scrollspeed = 16; // how fast to scroll
scrollarea = 4; // how close to the side of the screen
frameskip = 1;
type
TIsoTile = record
image: byte;
comment: string;
end;
type
TIsoMap = array[0..maxmapwidth, 0..maxmapheight] of TIsoTile;
type
TMainForm = class(TDXForm)
DXDraw: TDXDraw;
DXTimer: TDXTimer;
tiles: TDXImageList; // the images are loaded in the resource
image: TImage;
Label1: TLabel;
res: TComboBox;
bevel: TBevel;
nfo: TMemo;
go: TButton;
url: TLabel;
flip: TCheckBox; // used to be a TURLLabel
procedure DXDrawInitialize(Sender: TObject);
procedure DXDrawFinalize(Sender: TObject);
procedure DXTimerTimer(Sender: TObject; LagCount: Integer);
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure FormCreate(Sender: TObject);
procedure goClick(Sender: TObject);
procedure FormKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
IsoMap: TIsoMap; // our map
xoffset, yoffset: integer; // where map begins on screen
movex, movey: integer; // super smooth acelleration
kup, kdown, kleft, kright: boolean; // keyboard scrolling
end;
var
MainForm: TMainForm;
implementation
{$R *.DFM}
procedure TMainForm.FormCreate(Sender: TObject);
begin
tiles.Items.MakeColorTable; // if you use 256 color images for everything
DXDraw.ColorTable := tiles.Items.ColorTable; // use this for a glogal palette
DXDraw.DefColorTable := tiles.Items.ColorTable;
DXDraw.UpdatePalette;
res.ItemIndex := 7; // set default resolution
randomize;
end;
procedure TMainForm.DXDrawInitialize(Sender: TObject);
var loopx, loopy: integer;
begin
for loopx := 0 to maxmapwidth do
for loopy := 0 to maxmapheight do
IsoMap[loopx, loopy].image := 0; // reset map to all grass
for loopx := 0 to 6000 do
IsoMap[random(maxmapwidth), random(maxmapheight)].image := random(3); // make random map
xoffset := maxmapwidth * isowidth div 2;
yoffset := maxmapheight * isoheight div 4;
DXTimer.Enabled := True; // start game loop
DxDraw.Cursor := crnone;
end;
procedure TMainForm.DXDrawFinalize(Sender: TObject);
begin
DXTimer.Enabled := False;
end;
procedure TMainForm.DXTimerTimer(Sender: TObject; LagCount: Integer);
var
loopx, loopy,
mx, my: integer;
begin
if not DXDraw.CanDraw then Exit;
// DXDraw.Surface.Fill(0); // clear screen
for loopy := 0 to maxmapheight do
for loopx := 0 to maxmapwidth do
if abs((loopy * (isoheight div 2)) - yoffset)
< ((dxdraw.height div 2) + (isowidth * 2)) then // toss out tiles that are
if abs((loopx * isowidth) + ((loopy mod 2) * (isowidth div 2)) - xoffset)
< (dxdraw.Width) then // not partially visable
tiles.Items[IsoMap[loopx, loopy].image].draw( // this is a DXImageList
dxdraw.surface, // back surface
(loopx * isowidth) + ((loopy mod 2) * (isowidth div 2))
- xoffset, // offset x
(loopy * (isoheight div 2))
- yoffset, // offset y
0); // used later for animation
mx := mouse.CursorPos.x; // set mouse x,y
my := mouse.CursorPos.y;
if (mx > (dxdraw.width - scrollarea)) or kright then inc(movex, 4 * frameskip); // keyboard scroll
if (my > (dxdraw.height - scrollarea)) or kdown then inc(movey, 4 * frameskip);
if (mx < scrollarea) or kleft then dec(movex, 4 * frameskip);
if (my < scrollarea) or kup then dec(movey, 4 * frameskip);
if movey > 0 then dec(movey, 2 * frameskip); // accelerate scrolling
if movey < 0 then inc(movey, 2 * frameskip);
if movex > 0 then dec(movex, 2 * frameskip);
if movex < 0 then inc(movex, 2 * frameskip);
if movex > scrollspeed * scrollspeed * frameskip then movex := scrollspeed * scrollspeed * frameskip; // cap speed
if movey > scrollspeed * scrollspeed * frameskip then movey := scrollspeed * scrollspeed * frameskip;
if movex < -scrollspeed * scrollspeed * frameskip then movex := -scrollspeed * scrollspeed * frameskip;
if movey < -scrollspeed * scrollspeed * frameskip then movey := -scrollspeed * scrollspeed * frameskip;
xoffset := xoffset + movex div scrollspeed; // apply changes
yoffset := yoffset + movey div scrollspeed;
if xoffset > ((maxmapwidth * isowidth) - safeedge) then // safe edge check
xoffset := ((maxmapwidth * isowidth) - safeedge);
if xoffset < safeedge then xoffset := safeedge;
if yoffset > ((maxmapheight * isoheight) div 2 - safeedge) then
yoffset := ((maxmapheight * isoheight) div 2 - safeedge);
if yoffset < safeedge then yoffset := safeedge;
with DXDraw.Surface.Canvas do // FPS counter
begin
Brush.Style := bsClear;
Font.Color := clWhite;
Font.Size := 8;
Textout(30, 30, 'FPS: ' + inttostr(DXTimer.framerate));
Release;
end;
tiles.Items.Find('mouse').Draw(DXDraw.surface, mx, my, 0); // mouse pointer
DXDraw.Flip; // show screen
end;
procedure TMainForm.goClick(Sender: TObject);
var
s: string;
i, AWidth, AHeight, ABitCount: Integer;
begin
nfo.Hide; // hide everything else on the form
image.hide; // should have put everything on a
label1.hide; // panel and hid that
res.hide;
go.hide;
url.hide;
bevel.hide;
flip.Hide;
mainform.BorderStyle := bsNone; // use this to go from launch form
Dxdraw.Align := alClient; // to full screen
if flip.Checked then begin
Dxdraw.Options:=Dxdraw.Options + [doFlip]; // flip page
Dxdraw.Options:=Dxdraw.Options + [doWaitVBlank]; // wait retrace
end;
s := res.Items.Strings[res.Itemindex];
i := Pos('x', s);
AWidth := StrToInt(Copy(s, 1, i - 1)); // get width
s := Copy(s, i + 1, Length(s));
i := Pos('x', s);
AHeight := StrToInt(Copy(s, 1, i - 1)); // get height
s := Copy(s, i + 1, Length(s));
ABitCount := StrToInt(s); // and bit count
DXDraw.Display.Width := AWidth;
DXDraw.Display.Height := AHeight;
DXDraw.Display.BitCount := ABitCount;
Dxdraw.Initialize; // set all that up yourself then INIT
end;
procedure TMainForm.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if Key = VK_ESCAPE then // or you could go back to the launch pad
Close;
if (ssAlt in Shift) and (Key = VK_RETURN) then // Screen mode change
begin
DXDraw.Finalize; // basic ALT-ENTER stuff
if doFullScreen in DXDraw.Options then
begin
RestoreWindow;
DXDraw.Cursor := crDefault;
BorderStyle := bsSizeable;
DXDraw.Options := DXDraw.Options - [doFullScreen];
end
else
begin
StoreWindow;
DXDraw.Cursor := crNone;
BorderStyle := bsNone;
DXDraw.Options := DXDraw.Options + [doFullScreen];
end;
DXDraw.Initialize;
end;
if Key = VK_up then kup := true; // *** scroll up ***
if Key = VK_down then kdown := true; // *** scroll down ***
if Key = VK_right then kright := true; // *** scroll right ***
if Key = VK_left then kleft := true; // *** scroll left ***
end;
procedure TMainForm.FormKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if Key = VK_up then kup := false; // *** scroll up ***
if Key = VK_down then kdown := false; // *** scroll down ***
if Key = VK_right then kright := false; // *** scroll right ***
if Key = VK_left then kleft := false; // *** scroll left ***
end;
end.