Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit Unit1;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, dglOpenGL, Vcl.ExtCtrls, Vcl.StdCtrls, Vcl.Menus, Vcl.ToolWin,
- Vcl.ComCtrls, Vcl.Samples.Spin, Vcl.Tabs, Vcl.CheckLst, Vcl.ActnMan,
- Vcl.ActnColorMaps, Vcl.ExtDlgs, Vcl.ImgList;
- type
- TForm1 = class(TForm)
- MainMenu1: TMainMenu;
- N1: TMenuItem;
- N2: TMenuItem;
- N3: TMenuItem;
- N4: TMenuItem;
- N5: TMenuItem;
- N6: TMenuItem;
- N7: TMenuItem;
- N8: TMenuItem;
- N9: TMenuItem;
- N10: TMenuItem;
- PopupMenu1: TPopupMenu;
- N12: TMenuItem;
- N13: TMenuItem;
- N14: TMenuItem;
- N15: TMenuItem;
- StatusBar1: TStatusBar;
- TrayIcon1: TTrayIcon;
- Panel1: TPanel;
- PaintBox1: TPaintBox;
- Timer1: TTimer;
- FileOpenDialog1: TFileOpenDialog;
- FileSaveDialog1: TFileSaveDialog;
- BalloonHint1: TBalloonHint;
- PageControl1: TPageControl;
- TabSheet1: TTabSheet;
- TabSheet2: TTabSheet;
- TabSheet3: TTabSheet;
- ScrollBox1: TScrollBox;
- GroupBox1: TGroupBox;
- CheckBox1: TCheckBox;
- SpinEdit1: TSpinEdit;
- SpinEdit2: TSpinEdit;
- ScrollBox2: TScrollBox;
- GroupBox2: TGroupBox;
- ToolBar1: TToolBar;
- ComboBox1: TComboBox;
- ToolButton1: TToolButton;
- ToolButton2: TToolButton;
- ToolButton3: TToolButton;
- ToolButton4: TToolButton;
- ToolButton5: TToolButton;
- ToolButton6: TToolButton;
- ToolButton7: TToolButton;
- ToolButton8: TToolButton;
- ToolButton9: TToolButton;
- ToolButton10: TToolButton;
- ToolButton11: TToolButton;
- ImageList1: TImageList;
- procedure FormResize(Sender: TObject);
- procedure FormDestroy(Sender: TObject);
- procedure IdleHandler(Sender: TObject; var Done : Boolean);
- procedure InitMode(M_type: integer);
- procedure FormCreate(Sender: TObject);
- procedure TrayIcon1DblClick(Sender: TObject);
- procedure FormClose(Sender: TObject; var Action: TCloseAction);
- procedure FormHide(Sender: TObject);
- procedure PaintBox1Paint(Sender: TObject);
- procedure Timer1Timer(Sender: TObject);
- procedure PaintBox1MouseDown(Sender: TObject; Button: TMouseButton;
- Shift: TShiftState; X, Y: Integer);
- procedure PaintBox1Click(Sender: TObject);
- procedure N4Click(Sender: TObject);
- procedure N3Click(Sender: TObject);
- procedure N2Click(Sender: TObject);
- procedure N6Click(Sender: TObject);
- procedure N5Click(Sender: TObject);
- procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
- procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
- Shift: TShiftState; X, Y: Integer);
- private
- { Private declarations }
- procedure SetupGL;
- public
- { Public declarations }
- dc : HDC;
- hrc : HGLRC;
- rotation: real;
- bx,by:integer;
- createcontext: boolean;
- end;
- const
- NearClipping = 0.1; //Ближняя плоскость отсечения
- FarClipping = 200; //Дальняя плоскость отсечения
- g = 9.8;
- type
- Rectangle=record
- x: real;
- y: real;
- width: integer;
- height: integer;
- end;
- Objects = record
- box : Rectangle;
- lives : integer;
- typei:integer;
- Ttype: integer;
- Collision: Boolean;
- draw: integer;
- end;
- Textures = record
- gobject:array[1..5] of glUint;
- end;
- var
- Form1: TForm1;
- mode: integer=1;
- camera_x: extended=0.0;
- camera_y: extended=0.0;
- mouse_up: boolean=true;
- mouse_down, mouse_move: boolean;
- box: rectangle;
- gameobj: array[1..5000] of objects;
- useSnap: boolean;
- snapstepy, snapstepx: integer;
- gobj_count: longint;
- filename: string='0';
- filelable: string='0';
- mouse_x, mouse_y, mouse_mx, mouse_my: integer;
- Tex: Textures;
- implementation
- {$R *.dfm}
- function Texture_Init(path:pchar):GLuint;
- var
- i:longint;
- gBitmap:hBitmap;
- sBitmap:Bitmap;
- TextureID : GLuint;
- format: GLuint;
- begin
- writeln(path);
- gbitmap:=LoadImage(GetModuleHandle(NIL), path, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION or LR_LOADFROMFILE);
- GetObject(gbitmap, sizeof(sbitmap), @sbitmap);
- glEnable(GL_TEXTURE_2D);
- glGenTextures(1,@TextureID);
- glBindTexture(GL_TEXTURE_2D,TextureID);
- glPixelStorei(GL_UNPACK_ALIGNMENT,4);
- glPixelStorei(GL_UNPACK_ROW_LENGTH,0);
- glPixelStorei(GL_UNPACK_SKIP_ROWS,0);
- glPixelStorei(GL_UNPACK_SKIP_PIXELS,0);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- if sbitmap.bmBitsPixel / 8 = 4 then begin
- format:=GL_BGRA_EXT;
- end else begin
- format:=GL_BGR_EXT;
- end;
- glTexImage2D(GL_TEXTURE_2D, 0, sbitmap.bmBitsPixel div 8, sbitmap.bmWidth, sbitmap.bmHeight,
- 0, format, GL_UNSIGNED_BYTE, sbitmap.bmBits);
- glBindTexture(GL_TEXTURE_2D, TextureID);
- Texture_Init:=TextureID;
- end;
- procedure TextInit;
- begin
- Tex.gobject[1]:=Texture_Init('Data\Images\1.bmp');
- end;
- procedure TForm1.SetupGL;
- begin
- glClearColor(0.3,0.4,0.7,0.0); // цвет фона голубой
- glEnable(GL_DEPTH_TEST); //включить режим тест глубины
- glEnable(GL_CULL_FACE); //включить режим отображения только передних поверхностей
- end;
- procedure TForm1.Timer1Timer(Sender: TObject);
- begin
- PaintBox1Paint(Sender);
- end;
- procedure TForm1.TrayIcon1DblClick(Sender: TObject);
- begin
- ShowWindow(Application.Handle, SW_RESTORE);
- end;
- procedure TForm1.IdleHandler(Sender : TObject; var Done : Boolean);
- begin
- Sleep(1);
- Done := False;
- end;
- procedure DrawGrid(size: real; tWidth: integer; tHeight: integer);
- var i: integer;
- begin
- glBegin(GL_LINES);
- for i:=0 to tWidth do
- begin
- if (i=0) then glColor3f(0.6,0.3,0.3)
- else glColor3f(0.25,0.25,0.25);
- glVertex3f(0,i*size,0);
- glVertex3f(tWidth,i*size,0);
- end;
- for i := 0 to tHeight do
- begin
- if (i=0) then glColor3f(0.3,0.3,0.6)
- else glColor3f(0.25,0.25,0.25);
- glVertex3f(i*size,0,0);
- glVertex3f(i*size,tHeight,0);
- end;
- glEnd();
- end;
- procedure draw_quad(_x, _y, _width, _height :real);
- begin
- glBegin( GL_QUADS );
- glTexCoord2f(0.0, 0.0); glVertex2f(_x, _y + _height);
- glTexCoord2f(1.0, 0.0); glVertex2f(_x + _width, _y + _height);
- glTexCoord2f(1.0, 1.0); glVertex2f(_x + _width, _y);
- glTexCoord2f(0.0, 1.0); glVertex2f(_x, _y);
- glEnd();
- end;
- procedure TForm1.InitMode(M_type: integer); //Мы можем работать в двух режимах, либо в ортографическом либо в перспективной проекции
- begin
- if (M_type=1)
- then
- begin
- glViewport(0,0,ClientWidth,ClientHeight);
- //установка видовой и проекционной матриц //
- glmatrixmode(GL_PROJECTION); //работаем в режиме проекционной матрицы
- glloadidentity(); //замещаем текущую матрицу не единичную
- glortho(0, ClientWidth, ClientHeight, 0, 0, 1); //заружаем ортогональную проекционную матрицу
- glmatrixmode(GL_MODELVIEW); //работаем в режиме объектно-видовой матрицы
- glloadidentity(); //заменяем текущую матрицу на единичную
- gltranslatef(camera_x, camera_y, 0); //смещаем текущую матрицу (хак, чтобы тексели попадали в пиксели)
- glDisable(GL_DEPTH_TEST); //отключаем проверку буфера глубины
- glEnable(GL_CULL_FACE); //включаем отсечение задних граней
- glCullFace(GL_BACK); //отсекаться будут задние грани (повёрнутые задом к камере)
- glFrontFace(GL_CCW); //верншины полигонов должны задаваться в порядке "против часовой стрелки"
- glShadeModel(GL_SMOOTH); //устанавливаем модель шейдинга
- end;
- end;
- procedure TForm1.N2Click(Sender: TObject);
- var
- i:integer;
- begin
- filename:='0';
- filelable:='0';
- for I := 1 to 5000 do
- begin
- gameobj[i].box.width:=0;
- gameobj[i].box.height:=0;
- gameobj[i].box.x:=0;
- gameobj[i].box.y:=0;
- gameobj[i].typei:=0;
- gameobj[i].lives:=0;
- gameobj[i].draw:=0;
- gameobj[i].Ttype:=0;
- gameobj[i].Collision:=false;
- end;
- gobj_count:=0;
- end;
- procedure TForm1.N3Click(Sender: TObject);
- var f:textfile; i: integer;
- begin
- if filename='0' then
- begin
- if FileSaveDialog1.Execute then
- begin
- filename:=FileSaveDialog1.FileName;
- filelable:=FileSaveDialog1.FileNameLabel;
- for I := 1 to length(filename) do
- if (filename[i]='.') and (filename[i+1]='t') and (filename[i+2]='x')
- and (filename[i+3]='t')
- then AssignFile(f,filename) ;
- Rewrite(f);
- for i := 1 to 5000 do
- begin
- Writeln(f, gameobj[i].box.x:2:4,' ',
- gameobj[i].box.y:2:4,' ',
- gameobj[i].box.width,' ',
- gameobj[i].box.height,' ',
- gameobj[i].typei,' ',
- gameobj[i].draw,' ',
- gameobj[i].lives,' ',
- gobj_count)
- end;
- CloseFile(f);
- end;
- end ;
- end;
- procedure TForm1.N4Click(Sender: TObject);
- var f: textfile; i: integer;
- begin
- if FileSaveDialog1.Execute then
- begin
- filename:=FileSaveDialog1.FileName;
- filelable:=FileSaveDialog1.FileNameLabel;
- for I := 1 to length(filename) do
- if (filename[i]='.') and (filename[i+1]='t') and (filename[i+2]='x') and
- (filename[i+3]='t')
- then AssignFile(f,filename)
- else AssignFile(f,filename+'.txt');
- Rewrite(f);
- for i := 1 to 5000 do
- begin
- Writeln(f, gameobj[i].box.x:2:4,' ',
- gameobj[i].box.y:2:4,' ',
- gameobj[i].box.width,' ',
- gameobj[i].box.height,' ',
- gameobj[i].typei,' ',
- gameobj[i].draw,' ',
- gameobj[i].lives,' ',
- gobj_count)
- end;
- CloseFile(f);
- end;
- end;
- procedure TForm1.N5Click(Sender: TObject);
- var
- i:integer; f: textfile;
- begin
- if FileOpenDialog1.Execute then
- begin
- filename:='0';
- filelable:='0';
- for I := 1 to 5000 do
- begin
- gameobj[gobj_count].box.width:=0;
- gameobj[gobj_count].box.height:=0;
- gameobj[gobj_count].box.x:=0;
- gameobj[gobj_count].box.y:=0;
- gameobj[gobj_count].typei:=0;
- gameobj[gobj_count].lives:=0;
- gameobj[gobj_count].draw:=0;
- gameobj[gobj_count].Ttype:=0;
- gameobj[gobj_count].Collision:=false;
- end;
- gobj_count:=0;
- filename:=FileOpenDialog1.FileName;
- filelable:=FileOpenDialog1.FileNameLabel;
- AssignFile(f, filename);
- Reset(f);
- for I := 1 to 5000 do
- readln(f, gameobj[i].box.x,
- gameobj[i].box.y,
- gameobj[i].box.width,
- gameobj[i].box.height,
- gameobj[i].typei,
- gameobj[i].draw,
- gameobj[i].lives,
- gobj_count);
- CloseFile(f);
- end;
- end;
- procedure TForm1.N6Click(Sender: TObject);
- begin
- PostQuitMessage(0);
- end;
- procedure TForm1.PaintBox1Click(Sender: TObject);
- var
- I: Integer;
- begin
- end;
- procedure TForm1.PaintBox1MouseDown(Sender: TObject; Button: TMouseButton;
- Shift: TShiftState; X, Y: Integer);
- var bricks_count: integer;
- begin
- end;
- procedure Scene_Draw();
- var i:integer;
- begin
- for I := 1 to 5000 do
- draw_quad(gameobj[i].box.x,
- gameobj[i].box.y,
- gameobj[i].box.width,
- gameobj[i].box.height);
- end;
- procedure TForm1.PaintBox1Paint(Sender: TObject);
- begin
- glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
- InitMode(mode);
- if checkbox1.Checked=true then
- DrawGrid(SpinEdit2.Value, SpinEdit1.Value, SpinEdit1.Value);
- Scene_Draw;
- if GetKeyState(38) and 128>=128 then
- begin
- camera_y:=camera_y+2
- end;
- if GetKeyState(40) and 128>=128 then
- begin
- camera_y:=camera_y-2
- end;
- if GetKeyState(37) and 128>=128 then
- begin
- camera_x:=camera_x+2
- end;
- if GetKeyState(39) and 128>=128 then
- begin
- camera_x:=camera_x-2
- end;
- //InvalidateRect(Handle,nil,false);
- if filename='0' then StatusBar1.Panels.Items[4].Text:='* NEW FILE'
- else StatusBar1.Panels.Items[4].Text:=filename;
- StatusBar1.Panels.Items[0].Text:='X: '+IntToStr(mouse_x);
- StatusBar1.Panels.Items[1].Text:='Y: '+IntToStr(mouse_y);
- StatusBar1.Panels.Items[3].Text:=IntToStr(gobj_count);
- SwapBuffers(dc);
- end;
- procedure TForm1.FormResize(Sender: TObject);
- var
- tmpBool : Boolean;
- begin
- InitMode(mode);
- idleHandler(Sender,tmpBool);
- end;
- procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
- begin
- ShowWindow(Application.Handle, SW_HIDE);
- end;
- procedure TForm1.FormCreate(Sender: TObject);
- begin
- dc := GetDC(Panel1.Handle); //получаем контекст устройства по форме Form1
- //с InitOpenGL инициализирцется OpenGL, если это не удается то приложение закрывается
- if not InitOpenGL then
- Application.Terminate;
- //эта строка создаёт контекст рендеринга
- hrc := CreateRenderingContext(dc,[opDoubleBuffered],32,24,0,0,0,0);
- //TextInit;
- ActivateRenderingContext(dc,hrc); //активируем контекст рендеринга
- SetupGL; //установка режимов OpenGL
- Application.OnIdle := IdleHandler;
- end;
- procedure TForm1.FormDestroy(Sender: TObject);
- begin
- DeactivateRenderingContext;
- DestroyRenderingContext(hrc);
- ReleaseDC(Handle,dc);
- end;
- procedure TForm1.FormHide(Sender: TObject);
- begin
- ShowWindow(Application.Handle, SW_HIDE);
- end;
- procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
- Shift: TShiftState; X, Y: Integer);
- begin
- if ComboBox1.ItemIndex=0 then
- begin
- gobj_count:=gobj_count+1;
- gameobj[gobj_count].box.width:=32;
- gameobj[gobj_count].box.height:=32;
- gameobj[gobj_count].box.x:=mouse_x-camera_x;
- gameobj[gobj_count].box.y:=mouse_y-camera_y;
- gameobj[gobj_count].typei:=1;
- gameobj[gobj_count].lives:=1;
- gameobj[gobj_count].draw:=1;
- end;
- if ComboBox1.ItemIndex=1 then
- begin
- gobj_count:=gobj_count+1;
- gameobj[gobj_count].box.width:=32;
- gameobj[gobj_count].box.height:=32;
- gameobj[gobj_count].box.x:=mouse_x-camera_x;
- gameobj[gobj_count].box.y:=mouse_y-camera_y;
- gameobj[gobj_count].typei:=2;
- gameobj[gobj_count].lives:=1;
- gameobj[gobj_count].draw:=1;
- end;
- if ComboBox1.ItemIndex=2 then
- begin
- gobj_count:=gobj_count+1;
- gameobj[gobj_count].box.width:=32;
- gameobj[gobj_count].box.height:=32;
- gameobj[gobj_count].box.x:=mouse_x-camera_x;
- gameobj[gobj_count].box.y:=mouse_y-camera_y;
- gameobj[gobj_count].typei:=3;
- gameobj[gobj_count].lives:=1;
- gameobj[gobj_count].draw:=1;
- end;
- if ComboBox1.ItemIndex=3 then
- begin
- gobj_count:=gobj_count+1;
- gameobj[gobj_count].box.width:=32;
- gameobj[gobj_count].box.height:=32;
- gameobj[gobj_count].box.x:=mouse_x-camera_x;
- gameobj[gobj_count].box.y:=mouse_y-camera_y;
- gameobj[gobj_count].typei:=4;
- gameobj[gobj_count].lives:=1;
- gameobj[gobj_count].draw:=1;
- end;
- if ComboBox1.ItemIndex=4 then
- begin
- gobj_count:=gobj_count+1;
- gameobj[gobj_count].box.width:=32;
- gameobj[gobj_count].box.height:=32;
- gameobj[gobj_count].box.x:=mouse_x-camera_x;
- gameobj[gobj_count].box.y:=mouse_y-camera_y;
- gameobj[gobj_count].typei:=5;
- gameobj[gobj_count].lives:=1;
- gameobj[gobj_count].draw:=1;
- end;
- if ((GetKeyState(97) and 128)>=128) and (mouse_x>gameobj[gobj_count].box.x)
- and (mouse_x<gameobj[gobj_count].box.x + gameobj[gobj_count].box.width)
- and (mouse_y<gameobj[gobj_count].box.y + gameobj[gobj_count].box.height)
- and (mouse_y>gameobj[gobj_count].box.y)
- then
- begin
- gameobj[gobj_count].box.width:=0;
- gameobj[gobj_count].box.height:=0;
- gameobj[gobj_count].box.x:=0;
- gameobj[gobj_count].box.y:=0;
- gameobj[gobj_count].typei:=0;
- gameobj[gobj_count].lives:=0;
- gameobj[gobj_count].draw:=0;
- gameobj[gobj_count].Ttype:=0;
- gameobj[gobj_count].Collision:=false;
- gobj_count:=gobj_count-1;
- end;
- end;
- procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
- Y: Integer);
- begin
- if (GetKeyState(17) and 128)>=128 then
- begin
- camera_x:= camera_x - (mouse_x / 1000);
- camera_y:= camera_y - (mouse_y / 1000);
- end;
- if CheckBox1.Checked=true then
- begin
- mouse_x:=round(x/SpinEdit2.Value)*SpinEdit2.Value;
- mouse_y:=round(y/SpinEdit2.Value)*SpinEdit2.Value;
- end
- else begin
- mouse_x:=x;
- mouse_y:=y;
- end;
- end;
- end.
Add Comment
Please, Sign In to add comment