Создание 2d игры
02.01.2010, 23:43. Показов 3423. Ответов 1
Я создаю 2d игру. Пока пишу редактор карт. Пока у меня проблемы с изменением масштаба карты. Я выкладываю весь код своего редактора. Помогите мне найти ошибку в функции "void __fastcall SetMapSize(int width,int height,int new_number_of_tile)"  Если есть предложения по улучшению кода, пишите сюда. Задавайте ВСЕ вопросы здесь.
Main:
| C++ | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
| //---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "render.h"
#include "btn.h"
#include "level.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
TRender Render;
TBtn Btn;
TLevel Level;
int md=0;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
DoubleBuffered=true;
Render.SetBackbuffer(ClientWidth-Panel1->Width,ClientHeight);
Render.SetBackground(ClientWidth-Panel1->Width,ClientHeight);
int t1=Level.CountOfTiles("tiles.bmp",32,32);
if(t1>=97)
{
ShellExecute(0,0,"debug.exe","debug_info.txt 20 Function:Main:::send_warning_message:t1_value_very_much(max_96):::(Created_by____debug.exe):::",0,1);
Application->Terminate();
}
Level.TilesInit(t1,32,32);
Level.TilesLoad("tiles.bmp");
Level.edit_tile=33;
t1=0;
Level.InitTOM(Level.VOPSM(ClientWidth,ClientHeight));
Level.SetMapImgSize(ClientWidth,ClientHeight);
Level.NewMap(33);
Level.map_x=0;
Level.map_y=0;
//---------------------------------------------------------------------------
Btn.BtnInit(Level.tiles,Level.tile_w,Level.tile_h,Panel1);
int tx=0,ty=0;
t1=6;
Panel1->Width=t1*Btn.w;
try
{
for(int i=0;i<Btn.btns;i++)
{
Btn.BtnPaint(Level.tile[i],i);
if(tx>=Panel1->Width)
{
tx=0;
ty+=Btn.h;
}
Btn.btn[i]->Top=ty;
Btn.btn[i]->Left=tx;
Btn.btn[i]->ShowHint=true;
Btn.btn[i]->Tag=i;
Btn.btn[i]->Hint="Tile - "+IntToStr(i+1);
tx+=Btn.w;
}
}catch(...)
{
ShellExecute(0,0,"debug.exe","debug_info.txt 20 Function:Main:::send_error_message:out_of_resources:::(Created_by____debug.exe):::",0,1);
Application->Terminate();
}
Timer1->Enabled=true;
}
//---------------------------------------------------------------------------
void __fastcall TBtn::btnMouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
TImage *btn;
int id;
btn = (TImage*)Sender;
id=btn->Tag;
btn->Top+=1;
btn->Left+=1;
btn->Width+=1;
btn->Height+=1;
Level.edit_tile=id;
}
//---------------------------------------------------------------------------
void __fastcall TBtn::btnMouseUp(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
TImage *btn;
btn = (TImage*)Sender;
btn->Top-=1;
btn->Left-=1;
btn->Width-=1;
btn->Height-=1;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormResize(TObject *Sender)
{
Render.SetBackbuffer(ClientWidth-Panel1->Width,ClientHeight);
Render.SetBackground(ClientWidth-Panel1->Width,ClientHeight);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
try
{
Render.PaintOn(Level.map,Level.map_x,Level.map_y);
TPoint mp=ScreenToClient(Mouse->CursorPos);
if(Level.IsPosOnMap(mp.x,mp.y)==true)
{
int tx=int((mp.x)/Level.tile_w)*Level.tile_w;
int ty=int((mp.y)/Level.tile_h)*Level.tile_h;
Render.PaintOn(Level.tile[Level.edit_tile],tx,ty);
Caption=IntToStr(Level.WhatIsThisTileSelected(tx,ty)+1);
if(md==1) Level.PaintOnMap(tx,ty);
}
Render.Render(Canvas);
}catch(...)
{
Timer1->Enabled=true;
ShellExecute(0,0,"debug.exe","debug_info.txt 20 Function:MainTimer:::send_error_message:___error___:::(Created_by____debug.exe):::",0,1);
Application->Terminate();
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormMouseDown(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
md=1;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormMouseUp(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
md=0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key,
TShiftState Shift)
{
if(Key==VK_SPACE)
{
if(Level.map_w>=1500||Level.map_h>=1500)
{
ShowMessage("Max!");
return;
}
Timer1->Enabled=false;
Level.SetMapSize(Level.map_w+64,Level.map_h+64,33);
Application->ProcessMessages();
Timer1->Enabled=true;
}
if(Key==VK_UP) Level.map_y-=Level.tile_h;
if(Key==VK_DOWN) Level.map_y+=Level.tile_h;
if(Key==VK_LEFT) Level.map_x-=Level.tile_w;
if(Key==VK_RIGHT) Level.map_x+=Level.tile_w;
}
//--------------------------------------------------------------------------- |
|
Class TLevel
| C++ | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
| #include "math.h"
class TLevel
{
public:
Graphics::TBitmap**tile;
Graphics::TBitmap *map;
int *tile_on_map;
int tiles;
int map_w;
int map_h;
int map_x;
int map_y;
int edit_tile;
int tile_w;
int tile_h;
int size_map;
TLevel()
{
map_x=0;
map_y=0;
map=new Graphics::TBitmap;
edit_tile=33;
}
//BLOCK___TILES
int CountOfTiles(AnsiString img_file_name,int width_tile,int height_tile)
{
Graphics::TBitmap *img=new Graphics::TBitmap;
try{img->LoadFromFile(img_file_name);}catch(...)
{
ShellExecute(0,0,"debug.exe","debug_info.txt 20 Function:CountOfTiles(img_file_name):::send_error_message:img_file_name___not_found:::(Created_by____debug.exe):::",0,1);
Application->Terminate();
return 0;
}
int w1=int(img->Width/width_tile);
int h1=int(img->Height/height_tile);
delete img;
return w1*h1;
}
void __fastcall TilesInit(int count_of_tiles,int width_tile,int height_tile)
{
tiles=count_of_tiles;
tile_w=width_tile;
tile_h=height_tile;
try
{
tile = new Graphics::TBitmap*[tiles];
for(int i=0;i<tiles;i++)
{
tile[i] = new Graphics::TBitmap;
tile[i]->Width=tile_w;
tile[i]->Height=tile_h;
}
}catch(...)
{
ShellExecute(0,0,"debug.exe","debug_info.txt 20 Function:TilesInit(count_of_tiles,width_tile,height_tile):::send_error_message:out_of_resources:::(Created_by____debug.exe):::",0,1);
Application->Terminate();
}
}
void __fastcall TilesClear(int delete_mode)
{
if(delete_mode==0) ZeroMemory(&tile,sizeof(tile));
else if(delete_mode==1) delete []tile;
}
void __fastcall TilesLoad(AnsiString img_file_name)
{
Graphics::TBitmap *img=new Graphics::TBitmap;
try{img->LoadFromFile(img_file_name);}catch(...)
{
ShellExecute(0,0,"debug.exe","debug_info.txt 20 Function:TilesLoad(img_file_name):::send_error_message:img_file_name___not_found:::(Created_by____debug.exe):::",0,1);
Application->Terminate();
return;
}
try
{
int w1=int(img->Width/tile_w);
int h1=int(img->Height/tile_h);
int t1=0;
int tx=0,ty=0;
for(int i=0;i<tiles;i++)
{
if(t1<w1) t1++;
else { t1=1; tx=0; ty+=tile_h; }
tile[i]->Canvas->Draw(-tx,-ty,img);
tx+=tile_w;
}
}catch(...)
{
ShellExecute(0,0,"debug.exe","debug_info.txt 20 Function:TilesLoad(img_file_name):::send_error_message:out_of_resources:::(Created_by____debug.exe):::",0,1);
Application->Terminate();
}
delete img;
}
/*//END___BLOCK
----------------
*///BLOCK___MAP
int VOPSM(int width,int height)
{
int i1=int(width/tile_w);
int i2=int(height/tile_h);
return i1*i2;
}
void __fastcall InitTOM(int count)
{
tile_on_map = new int[count];
}
void __fastcall ClearTOM(int delete_mode)
{
if(delete_mode==0) ZeroMemory(&tile_on_map,sizeof(tile_on_map));
else if(delete_mode==1) delete []tile_on_map;
}
void __fastcall SetMapImgSize(int width,int height)
{
map_w=width;
map_h=height;
map->Width=map_w;
map->Height=map_h;
}
void __fastcall RepaintMap()
{
int tw=tile_w;
int th=tile_h;
int i1=int(map_w/tw);
int i2=int(map_h/th);
int tx=0;
int ty=0;
int t1=0;
for(int i=0;i<size_map;i++)
{
if(t1<i1) t1++;
else
{
t1=1;
tx=0;
ty+=th;
}
map->Canvas->Draw(tx,ty,tile[tile_on_map[i]]);
tx+=tw;
}
}
void __fastcall NewMap(int number_of_tile)
{
int tw=tile_w;
int th=tile_h;
int i1=int(map_w/tw);
int i2=int(map_h/th);
size_map=i1*i2;
for(int i=0;i<size_map;i++) tile_on_map[i]=number_of_tile;
RepaintMap();
}
int WhatIsThisTileSelected(int x,int y)
{
y=int(y/tile_h);
x=int(x/tile_w);
x-=ceil(map_x/tile_w);
y-=ceil(map_y/tile_h);
int t1=0;
for(int i=0;i<y;i++) t1+=int((map->Width-map_x)/tile_w);
t1+=x;
return t1;
}
bool IsPosOnMap(int x,int y)
{
int err=0;
if(x<=map_x) err=1;
else if(x-map_x>=map_w) err=1;
else if(y<=map_y) err=1;
else if(y-map_y>=map_h) err=1;
if(err==1) return false;
else return true;
}
void __fastcall SetMapSize(int width,int height,int new_number_of_tile) //Readme on russian lang. (RUS)
{
if(width>map_w||height>map_h)
{
//Ñïåðâà êîïèðóåì íîìåðà òàéëîâ
int new_i1=int(width/tile_w);
int new_i2=int(height/tile_h);
int *old_tile_on_map=new int[new_i1*new_i2];
for(int i=0;i<size_map;i++) old_tile_on_map[i]=tile_on_map[i];
ClearTOM(1); //Âñ¸... Âðîäå áû ñêîïèðîâàëè íîìåðà... Ìîæíî è óäàëèòü...
InitTOM(new_i1*new_i2); //...è ñðàçó æå ñîçäàòü çàíîâî, íî òîëüêî ñ íîâûìè ðàçìåðàìè
int tx=0,ty=0,t1=0;
for(int i=0;i<new_i1*new_i2;i++) tile_on_map[i]=new_number_of_tile; //Íîâûå íîìåðà
int t2=0;
TMemo *Memo=new TMemo(Application->Handle);
for(int i=0;i<new_i1*new_i2;i++)
{
if(t1<new_i1) t1++;
else
{
t1=1;
ty+=tile_h;
tx=0;
}
if(IsPosOnMap(tx+1,ty+1)==true)
{
int t2=WhatIsThisTileSelected(tx,ty);
tile_on_map[i]=old_tile_on_map[t2];
Memo->Lines->Add("tile_on_map["+IntToStr(i)+"]=old_tile_on_map["+IntToStr(t2)+"]="+IntToStr(old_tile_on_map[t2]));
}
tx+=tile_w;
}
Memo->Lines->SaveToFile("t1____t1.txt");
delete Memo;
delete []old_tile_on_map;
size_map=new_i1*new_i2;
SetMapImgSize(new_i1*tile_w,new_i2*tile_h);
RepaintMap();
}else{
int new_i1=int(width/tile_w);
int new_i2=int(height/tile_h);
int *old_tile_on_map=new int[size_map];
for(int i=0;i<size_map;i++) old_tile_on_map[i]=tile_on_map[i];
ClearTOM(1); //Âñ¸... Âðîäå áû ñêîïèðîâàëè íîìåðà... Ìîæíî è óäàëèòü...
InitTOM(new_i1*new_i2); //...è ñðàçó æå ñîçäàòü çàíîâî, íî òîëüêî ñ íîâûìè ðàçìåðàìè
//Ñðàçó æå íîâàÿ êàðòà
int tx=0,ty=0,t1=0;
for(int i=0;i<new_i1*new_i2;i++) tile_on_map[i]=new_number_of_tile; //Íîâûå íîìåðà
int t2=0;
TMemo *Memo=new TMemo(Application->Handle);
for(int i=0;i<new_i1*new_i2;i++)
{
if(t1<new_i1) t1++;
else
{
t1=1;
ty+=tile_h;
tx=0;
}
if(IsPosOnMap(tx+1,ty+1)==true)
{
int t2=WhatIsThisTileSelected(tx,ty);
tile_on_map[i]=old_tile_on_map[t2];
Memo->Lines->Add("tile_on_map["+IntToStr(i)+"]=old_tile_on_map["+IntToStr(t2)+"]="+IntToStr(old_tile_on_map[t2]));
}
tx+=tile_w;
}
Memo->Lines->SaveToFile("t1____t1.txt");
delete Memo;
delete []old_tile_on_map;
size_map=new_i1*new_i2;
SetMapImgSize(new_i1*tile_w,new_i2*tile_h);
RepaintMap();
}
}
void __fastcall PaintOnMap(int x,int y)
{
x-=map_x;
y-=map_y;
tile_on_map[WhatIsThisTileSelected(x,y)]=edit_tile;
map->Canvas->Draw(x,y,tile[tile_on_map[WhatIsThisTileSelected(x,y)]]);
}
}; |
|
Class TRender
| C++ | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
| class TRender
{
public:
Graphics::TBitmap *background;
Graphics::TBitmap *backbuffer;
int px;
int py;
TRender()
{
background=new Graphics::TBitmap();
backbuffer=new Graphics::TBitmap();
}
void __fastcall SetBackground(int w,int h)
{
background->Width=w;
background->Height=h;
background->Canvas->Pen->Color=clWhite;
background->Canvas->Brush->Color=clWhite;
background->Canvas->Rectangle(0,0,w,h);
}
void __fastcall SetBackbuffer(int w,int h)
{
backbuffer->Width=w;
backbuffer->Height=h;
}
void __fastcall PaintOn(Graphics::TBitmap *img,int x,int y)
{
backbuffer->Canvas->Draw(x,y,img);
}
void __fastcall Render(TCanvas *Canvas)
{
Canvas->Draw(px,py,backbuffer);
backbuffer->Canvas->Draw(0,0,background);
}
void __fastcall RepaintForm(TCanvas *Canvas,int x,int y,int x1,int y1)
{
Canvas->Brush->Color=clGray;
Canvas->Pen->Color=clGray;
Canvas->Rectangle(x,y,x1,y1);
}
void __fastcall SetPos(int x,int y)
{
px=x;
py=y;
}
}; |
|
Class TBtn
| C++ | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
| class TBtn
{
public:
TImage **btn;
void __fastcall btnMouseDown(TObject *Sender,TMouseButton Button,TShiftState Shift,int X,int Y);
void __fastcall btnMouseUp(TObject *Sender,TMouseButton Button,TShiftState Shift,int X,int Y);
int btns;
int w;
int h;
void __fastcall BtnInit(int count_of_buttons,int width,int height,TWinControl *p)
{
btns=count_of_buttons;
w=width;
h=height;
try
{
btn = new TImage*[btns];
for(int i=0;i<btns;i++)
{
btn[i] = new TImage(p);
btn[i]->Parent=p;
btn[i]->Width=w;
btn[i]->Height=h;
btn[i]->OnMouseDown=btnMouseDown;
btn[i]->OnMouseUp=btnMouseUp;
}
}catch(...)
{
ShellExecute(0,0,"debug.exe","debug_info.txt 20 Function:BtnInit(count_of_buttons,width,height,AOwner):::send_error_message:out_of_resources:::(Created_by____debug.exe):::",0,1);
Application->Terminate();
}
}
void __fastcall BtnPaint(Graphics::TBitmap *img,int number_of_button)
{
try
{
int nb=number_of_button;
btn[nb]->Picture->Assign(img);
}catch(...)
{
ShellExecute(0,0,"debug.exe","debug_info.txt 20 Function:BtnPaint(img,number_of_button):::send_error_message:out_of_resources:::(Created_by____debug.exe):::",0,1);
Application->Terminate();
}
}
void __fastcall BtnClear()
{
for(int i=0;i<btns;i++) delete btn[i];
delete btn;
}
}; |
|
PS На
этот
| C++ | 1
2
| ShellExecute(0,0,"debug.exe","debug_info.txt 20 Function:Main:::send_warning_message:t1_value_very_much(max_96):::(Created_by____debug.exe):::",0,1);
Application->Terminate(); |
|
код или что-то похожее на него не обращайте внимания... Это код для создания debug-инфы
0
|