webForumDet fria alternativet

Krångligt problem

10 svar · 342 visningar · startad av Alpha II

Alpha IIMedlem sedan maj 20002 579 inlägg
#1

Jag vill ha min kod för att sätta upp ett OpenGL fönster i en speciell fil eftersom koden tar så stor plats och jag vill göra spel koden lite enklare att läsa.

Jag skapade en fil som jag kalade Init.pas och klistade in koden och modifierade den en aning. Men koden kallar procedurer som jag vill ska ligga i .dpr filen. I c++ verkar det enkelt med att inkludera filen. Men hur gör jag i delphi? måste jag lägga till dpr filen i uses? Vet ni om det finns någon färdig objekt baserad kod (koden jag använder är inte speciellt vacker)?

Alpha IIMedlem sedan maj 20002 579 inlägg
#2

Så här vill jag att init ska se ut

unit Init;

interface

uses
  Windows,
  Messages,
  OpenGL12;

type
  TWM = ( wmAsk, wmFullscreen, wmWindowed );

var
  h_RC         : HGLRC;
  h_DC         : HDC;
  h_Wnd        : HWND;
  Keys         : array[0..255] of BOOL;
  Active       : Boolean;
  FullScreen   : Boolean;
  WindowWidth  : Integer;
  WindowHeight : Integer;
  PixelDepth   : Integer;
  WindowTitle  : String;

procedure NewWindow(Title: String; Width, Height, Bits: Integer; WindowMode: TWM);

implementation

function WndProc(hWnd: HWND; Msg: UINT; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
begin
  if Msg = WM_SYSCOMMAND then
    begin
      case wParam of
        SC_SCREENSAVE, SC_MONITORPOWER:
          begin
            result := 0;
            exit;
          end;
      end;
    end;

  case Msg of
    WM_ACTIVATE:
      begin
        if (Hiword(wParam) = 0) then
          Active := true
        else
          Active := false;
        Result := 0;
      end;
    WM_CLOSE:
      Begin
        PostQuitMessage(0);
        Result := 0;
      end;
    WM_KEYDOWN:
      begin
        keys[wParam] := TRUE;
        result:=0;
      end;
    WM_KEYUP:
      begin
    	keys[wParam] := FALSE;
        Result := 0;
      end;
    WM_SIZE:
      begin
//    	ReSizeGLScene(LOWORD(lParam),HIWORD(lParam));
        Result := 0;
      end
  else
    begin
      Result := DefWindowProc(hWnd, Msg, wParam, lParam);
    end;
  end;
end;

procedure KillGLWindow(Fullscreen:Boolean);
begin
  CloseOpenGL;
  if Fullscreen then
  begin
    ChangeDisplaySettings(devmode(nil^), 0);
    ShowCursor(True);
  end;

  if (not wglMakeCurrent(h_DC, 0)) then
    MessageBox(0, 'Release of DC and RC failed!', 'Error', MB_OK or MB_ICONERROR);

  if (not wglDeleteContext(h_RC)) then
  begin
    MessageBox(0, 'Release of rendering context failed!', 'Error', MB_OK or MB_ICONERROR);
    h_RC := 0;
  end;

  if ((h_DC > 1) and (ReleaseDC(h_Wnd, h_DC) = 0)) then
  begin
    MessageBox(0, 'Release of device context failed!', 'Error', MB_OK or MB_ICONERROR);
    h_DC := 0;
  end;

  if ((h_Wnd <> 0) and (not DestroyWindow(h_Wnd))) then
  begin
    MessageBox(0, 'Unable to destroy window!', 'Error', MB_OK or MB_ICONERROR);
    h_Wnd := 0;
  end;

  if (not UnRegisterClass('OpenGL', hInstance)) then
  begin
    MessageBox(0, 'Unable to unregister window class!', 'Error', MB_OK or MB_ICONERROR);
    hInstance := 0;
  end;
end;

function WinMain(hInstance: HINST; hPrevInstance: HINST; lpCmdLine: PChar; nCmdShow: integer): integer; stdcall;
var
  Msg: TMsg;
  Done: Boolean;
begin
  Done := false;

  while (not Done) do
    begin
      if (PeekMessage(Msg, 0, 0, 0, PM_REMOVE)) then
        begin
          if Msg.Message = WM_QUIT then
            Done := true
          else
            begin
	      TranslateMessage(Msg);
	      DispatchMessage(Msg);
	    end;
        end
      else
        begin
          if keys[VK_ESCAPE] then
            Done := true
          else begin
//            glDrawScene();
            SwapBuffers(h_DC);

//            ProcessKeys();

            // Count FPS
//            FPS := FPS + 1;
//            if int(GetTickCount()) - LastTime > 1000 then
//              begin
//                SetWindowText(h_Wnd, PChar('FPS: ' + inttostr(FPS)));
//                FPS := 0;
//                LastTime := GetTickCount();
//              end;
          end;
        end;
    end;
  KillGLwindow(FALSE);
  Result := Msg.wParam;
end;

function CreateGLWindow(): Boolean;
var
  wndClass : TWndClass;
  dwStyle : DWORD;
  dwExStyle : DWORD;
  dmScreenSettings : DEVMODE;
  PixelFormat : GLuint;
  h_Instance : HINST;
  pfd : TPIXELFORMATDESCRIPTOR;
begin
  h_Instance := GetModuleHandle(nil);
  ZeroMemory(@wndClass, SizeOf(wndClass));

  with wndClass do
  begin
    style         := CS_HREDRAW or
                     CS_VREDRAW or
                     CS_OWNDC;
    lpfnWndProc   := @WndProc;
    hInstance     := h_Instance;
    hCursor       := LoadCursor(0, IDC_ARROW);
    lpszClassName := 'OpenGL';
  end;

  if (RegisterClass(wndClass) = 0) then
  begin
    MessageBox(0, 'Failed to register the window class!', 'Error', MB_OK or MB_ICONERROR);
    Result := False;
    Exit
  end;

  if Fullscreen then
  begin
    ZeroMemory(@dmScreenSettings, SizeOf(dmScreenSettings));
    with dmScreenSettings do begin
      dmSize       := SizeOf(dmScreenSettings);
      dmPelsWidth  := WindowWidth;
      dmPelsHeight := WindowHeight;
      dmBitsPerPel := PixelDepth;
      dmFields     := DM_PELSWIDTH or DM_PELSHEIGHT or DM_BITSPERPEL;
    end;

    if (ChangeDisplaySettings(dmScreenSettings, CDS_FULLSCREEN) = DISP_CHANGE_FAILED) then
    begin
      MessageBox(0, 'Unable to switch to fullscreen!', 'Error', MB_OK or MB_ICONERROR);
      Fullscreen := False;
    end;
  end;

  if (Fullscreen) then
  begin
    dwStyle := WS_POPUP or
               WS_CLIPCHILDREN
               or WS_CLIPSIBLINGS;
    dwExStyle := WS_EX_APPWINDOW;
    ShowCursor(False);
  end
  else
  begin
    dwStyle := WS_OVERLAPPEDWINDOW or
               WS_CLIPCHILDREN or
               WS_CLIPSIBLINGS;
    dwExStyle := WS_EX_APPWINDOW or
                 WS_EX_WINDOWEDGE;
  end;

  h_Wnd := CreateWindowEx(dwExStyle,
                          'OpenGL',
                          PChar(WindowTitle),
                          dwStyle,
                          0, 0,
                          WindowWidth, WindowHeight,
                          0,
                          0,
                          h_Instance,
                          nil);
  if h_Wnd = 0 then
  begin
    KillGLWindow(Fullscreen);
    MessageBox(0, 'Unable to create window!', 'Error', MB_OK or MB_ICONERROR);
    Result := False;
    Exit;
  end;

  h_DC := GetDC(h_Wnd);
  if (h_DC = 0) then
  begin
    KillGLWindow(Fullscreen);
    MessageBox(0, 'Unable to get a device context!', 'Error', MB_OK or MB_ICONERROR);
    Result := False;
    Exit;
  end;

  with pfd do
  begin
    nSize          := SizeOf(TPIXELFORMATDESCRIPTOR);
    nVersion        := 1;
    dwFlags         := PFD_DRAW_TO_WINDOW
                       or PFD_SUPPORT_OPENGL
                       or PFD_DOUBLEBUFFER;
    iPixelType      := PFD_TYPE_RGBA;
    cColorBits      := PixelDepth;
    cRedBits        := 0;
    cRedShift       := 0;
    cGreenBits      := 0;
    cGreenShift     := 0;
    cBlueBits       := 0;
    cBlueShift      := 0;
    cAlphaBits      := 0;
    cAlphaShift     := 0;
    cAccumBits      := 0;
    cAccumRedBits   := 0;
    cAccumGreenBits := 0;
    cAccumBlueBits  := 0;
    cAccumAlphaBits := 0;
    cDepthBits      := 16;
    cStencilBits    := 0;
    cAuxBuffers     := 0;
    iLayerType      := PFD_MAIN_PLANE;
    bReserved       := 0;
    dwLayerMask     := 0;
    dwVisibleMask   := 0;
    dwDamageMask    := 0;
  end;

  PixelFormat := ChoosePixelFormat(h_DC, @pfd);
  if (PixelFormat = 0) then
  begin
    KillGLWindow(Fullscreen);
    MessageBox(0, 'Unable to find a suitable pixel format', 'Error', MB_OK or MB_ICONERROR);
    Result := False;
    Exit;
  end;

  if (not SetPixelFormat(h_DC, PixelFormat, @pfd)) then
  begin
    KillGLWindow(Fullscreen);
    MessageBox(0, 'Unable to set the pixel format', 'Error', MB_OK or MB_ICONERROR);
    Result := False;
    Exit;
  end;

  h_RC := wglCreateContext(h_DC);
  if (h_RC = 0) then
  begin
    KillGLWindow(Fullscreen);
    MessageBox(0, 'Unable to create an OpenGL rendering context', 'Error', MB_OK or MB_ICONERROR);
    Result := False;
    Exit;
  end;

  if (not wglMakeCurrent(h_DC, h_RC)) then
  begin
    KillGLWindow(Fullscreen);
    MessageBox(0, 'Unable to activate OpenGL rendering context', 'Error', MB_OK or MB_ICONERROR);
    Result := False;
    Exit;
  end;

  InitOpenGL;

  ShowWindow(h_Wnd, SW_SHOW);
  SetForegroundWindow(h_Wnd);
  SetFocus(h_Wnd);

//  glResizeWnd(Width, Height);
//  glInit();

  Result := True;
end;

procedure NewWindow(Title: String; Width, Height, Bits: Integer; WindowMode: TWM);
begin
  case WindowMode of
  wmAsk:
   begin
    if MessageBox(0,'Would You Like To Run In FullScreen Mode?','Start FullScreen', MB_YESNO or MB_ICONQUESTION) = IDNO then
      FullScreen := false
    else
      FullScreen := true;
   end;
  wmFullscreen : Fullscreen := true;
  wmWindowed   : Fullscreen := false;
  end;

  if not CreateGLWindow() then
    begin
      Exit;
    end;

  Active := true;
  WinMain( hInstance, hPrevInst, CmdLine, CmdShow );
end;
end.
Alpha IIMedlem sedan maj 20002 579 inlägg
#3

Ingen som kan? :(
Det är viktigt!

BeatboxMedlem sedan okt. 20012 496 inlägg
#4

I .dpr-filen.

program Project;

uses
  Forms,
  Init in 'Init.pas';

{$R *.RES}

begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.

Och där du ska anropa funktionerna :

uses Init
Alpha IIMedlem sedan maj 20002 579 inlägg
#5

Jag förstår inte riktigt...
Jag vill ju anropa funktioner i dpr filen från pas filen

BeatboxMedlem sedan okt. 20012 496 inlägg
#6

Är inte .dpr-filen en sorts projektfil bara ? (Typ Delphi Project)

Alpha IIMedlem sedan maj 20002 579 inlägg
#7

Nepp.
MAn kan använda den på olika sätt. I spel används den oftast som "huvudfilen".

Så här ser min dpr fil ut just nu:

//*******************************//
//   © Copyright Daniel Henell   //
//      [email]noshit556@yahoo.com[/email]      //
//*******************************//

program Breakout3D_v01;

uses
  Windows,
  Init,
  OpenGL12,
  TGATexture;

const
  BLOCK_W = 1.0;
  BLOCK_H = 0.2;
  PADDLE_W = 1.2;
  PADDLE_H = 0.3;
  OBJ_DEPTH = 0.5;

type
  TVector3 = record
    X : GLfloat;
    Y : GLfloat;
    Z : GLfloat;
  end;

  TBlock = record
    Image    : Integer;
    Visible  : Boolean;
  end;

var
  // FPS vars
  LastFrame   : Integer;
  LastTime    : Integer;
  FPS         : Integer;

  // Textures
  BlockTex    : array[0..9] of GLuint;

  // Compiled objects
  Block       : GLuint;
  Ball        : GLuint;
  Paddle      : GLuint;

  // Game vars
  BlockMap    : array[0..9,0..9] of TBlock;
  PaddleX     : GLfloat;
  BallX       : GLfloat;
  BallY       : GLfloat;
  BallVelX    : GLfloat;
  BallVelY    : GLfloat;

function IntToStr(Num: Integer): String;
begin
  Str(Num, Result);
end;

function PixelPerSec(X : Integer): GLfloat;
begin
  Result := (x/1000)*(getTickCount()-LastFrame);
end;

procedure ReSizeGLScene(Width, Height: GLsizei);
begin
  WindowWidth := Width;
  WindowHeight := Height;

  if (Height = 0) then
    Height := 1;

  glViewport(0, 0, Width, Height);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(45.0, Width / Height, 0.1, 1500.0);

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
end;

procedure LoadBlockMap(Filename: String);
var
  F : file of byte;
  B : byte;
  X : Integer;
  Y : Integer;
begin
  AssignFile(F, Filename);
  Reset(F);
  for X := 0 to 9 do begin
  for Y := 0 to 9 do begin
    Read(F, B);
    if B > High(Block) then B := 0;
    BlockMap[X][Y].Image := B;
    BlockMap[X][Y].Visible := true;
  end;
  end;
  CloseFile(F);
end;

procedure CompileObjects();
var
  quadratic : PGLUquadricObj;
begin
  Block := glGenLists(1);
  glNewList(GL_COMPILE,Block);
  glBegin(GL_QUADS);
    glVertex3f(BLOCK_W, 0.0, 0.0);
    glVertex3f(0.0, 0.0, 0.0);
    glVertex3f(0.0, BLOCK_H, 0.0);
    glVertex3f(BLOCK_W, BLOCK_H, 0.0);

    glVertex3f(BLOCK_W, 0.0, -OBJ_DEPTH);
    glVertex3f(0.0, 0.0, -OBJ_DEPTH);
    glVertex3f(0.0, BLOCK_H, -OBJ_DEPTH);
    glVertex3f(BLOCK_W, BLOCK_H, -OBJ_DEPTH);

    glVertex3f(0.0, 0.0, 0.0);
    glVertex3f(0.0, 0.0, -OBJ_DEPTH);
    glVertex3f(0.0, BLOCK_H, -OBJ_DEPTH);
    glVertex3f(0.0, BLOCK_H, 0.0);

    glVertex3f(BLOCK_W, 0.0, 0.0);
    glVertex3f(BLOCK_W, 0.0, -OBJ_DEPTH);
    glVertex3f(BLOCK_W, BLOCK_H, -OBJ_DEPTH);
    glVertex3f(BLOCK_W, BLOCK_H, 0.0);
  glEnd();
  glEndList();

  Paddle := glGenLists(1);
  glNewList(GL_COMPILE,Paddle);
  glBegin(GL_QUADS);
    glVertex3f(PADDLE_W, 0.0, 0.0);
    glVertex3f(0.0, 0.0, 0.0);
    glVertex3f(0.0, PADDLE_H, 0.0);
    glVertex3f(PADDLE_W, PADDLE_H, 0.0);

    glVertex3f(PADDLE_W, 0.0, -OBJ_DEPTH);
    glVertex3f(0.0, 0.0, -OBJ_DEPTH);
    glVertex3f(0.0, PADDLE_H, -OBJ_DEPTH);
    glVertex3f(PADDLE_W, PADDLE_H, -OBJ_DEPTH);

    glVertex3f(0.0, 0.0, 0.0);
    glVertex3f(0.0, 0.0, -OBJ_DEPTH);
    glVertex3f(0.0, PADDLE_H, -OBJ_DEPTH);
    glVertex3f(0.0, PADDLE_H, 0.0);

    glVertex3f(PADDLE_W, 0.0, 0.0);
    glVertex3f(PADDLE_W, 0.0, -OBJ_DEPTH);
    glVertex3f(PADDLE_W, PADDLE_H, -OBJ_DEPTH);
    glVertex3f(PADDLE_W, PADDLE_H, 0.0);
  glEnd();
  glEndList();

  quadratic := gluNewQuadric();
  gluQuadricNormals(quadratic, GLU_SMOOTH);
  Ball := glGenLists(1);
  glNewList(GL_COMPILE,Ball);
    gluSphere(quadratic,1.3,32,32);
  glEndList();
end;

procedure glInit();
begin
  glClearColor(0.0, 0.0, 0.0, 0.0);
  glShadeModel(GL_SMOOTH);
  glClearDepth(1.0);
  glEnable(GL_DEPTH_TEST);
  glDepthFunc(GL_LESS);
  glEnable(GL_ALPHA_TEST);
  glAlphaFunc(GL_GREATER, 0.4);
  glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  glEnable(GL_TEXTURE_2D);

  LoadTGA('Data\Graphic\block000.tga', BlockTex[0], False);
  LoadTGA('Data\Graphic\block001.tga', BlockTex[1], False);
  LoadTGA('Data\Graphic\block002.tga', BlockTex[2], False);
  LoadTGA('Data\Graphic\block003.tga', BlockTex[3], False);
  LoadTGA('Data\Graphic\block004.tga', BlockTex[4], False);
  LoadTGA('Data\Graphic\block005.tga', BlockTex[5], False);
  LoadTGA('Data\Graphic\block006.tga', BlockTex[6], False);
  LoadTGA('Data\Graphic\block007.tga', BlockTex[7], False);
  LoadTGA('Data\Graphic\block008.tga', BlockTex[8], False);
  LoadTGA('Data\Graphic\block009.tga', BlockTex[9], False);

  LoadBlockMap('Data\Blocks.map');
end;

procedure DrawBlocks();
var
  X : Integer;
  Y : Integer;
begin
  for X := 0 to 9 do
  begin
    for Y := 0 to 9 do
    begin
      glPushMatrix();
      glTranslatef(X*BLOCK_W, Y*BLOCK_H, 0);
      glBindTexture(GL_TEXTURE_2D, BlockTex[BlockMap[X][Y].Image]);
      glCallList(Block);
      glPopMatrix();
    end;
  end;
end;

procedure glDrawScene();
begin
  LastFrame := getTickCount();
  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
  glLoadIdentity();

  DrawBlocks();

  glPushMatrix();
  glTranslatef(PaddleX, 0.0, 0.0);
  glCallList(Paddle);
  glPopMatrix();

  glPushMatrix();
  glTranslatef(BallX, BallY, 0.0);
  glCallList(Ball);
  glPopMatrix();
end;

procedure ProcessInput();
var
  MousePos : TPoint;
begin
  GetCursorPos(MousePos);
  if MousePos.x < WindowWidth shl 1 then PaddleX := PaddleX - PixelPerSec(100);
  if MousePos.x > WindowWidth shl 1 then PaddleX := PaddleX + PixelPerSec(100);
end;

begin
  NewWindow('Breakout3D',800,600,32,wmFullscreen);
end.
Alpha IIMedlem sedan maj 20002 579 inlägg
#8

iof skulle man kanske lägga koden för att skapa fönstert i dpr-filen och lägga spelet i en pas fil. :q´

Det borde väl gå?

jtMedlem sedan juni 2000326 inlägg
#9

>Det borde väl gå?
Jo, det borde inte vara några problem.
Utan att kollat på koden så borde det väl räcka med detta i dpr filen.

program Breakout3D_v01;

uses
Windows,
Init,
OpenGL12,
TGATexture;

begin
NewWindow('Breakout3D',800,600,32,wmFullscreen);
end.

Alpha IIMedlem sedan maj 20002 579 inlägg
#10

jo men jag hade tänkt att lägga spelet i dpr filen. fönster koden i Init.pas och diverse saker som att ladda textures i pas filer.

men jag tror jag ska lägga koden för att skapa ett fönster i dpr filen och spelet i Game.pas :)

Alpha IIMedlem sedan maj 20002 579 inlägg
#11

Nu har jag lagt spelkoden i Game.pas och WinAPI koden i dpr filen. I Game.pas behöver jag ta reda på fönsterets bredd med GetWindowRect(h_Wnd, WndRect) men det fungerar ju inte eftersom h_Wnd är deklarerad i dpr filen. Hur ska jag göra nu då?

Genererad på 487 ms · cache AV · v20260730165559-full.f96bc7eb