Ignis-sama
17.10.2007, 20:14
Hi, ich lern grad Spieleprogrammierung mit C++ und DirectX, hab ein paar interessante Beispiele, wo ich ein "Grundgerüst" genommen hab und nun versuche ein kleines 2D Spiel mit einer Map zu erstellen.
Angefangen hab ich mal so:
# include <stdio.h>
# include <windows.h>
# include <ddraw.h>
# include <dsound.h>
# include "ddutil.h"
# include "dsutil.h"
# include "resource.h"
class Tile
{
public:
int x;
int y;
Tile(){x = 0; y = 0;};
~Tile(){}
};
const int nettobreite = 640;
const int nettohoehe = 480;
const int anzahltiles = 12;
int bruttobreite;
int bruttohoehe;
int tilehoehe = 32;
int tilebreite = 128;
int ivy = 0;
int ivx;
Tile tilekoord[anzahltiles];
HINSTANCE lol_instance;
HWND lol_window;
HMENU lol_menu;
LRESULT CALLBACK lol_windowhandler( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_COMMAND:
switch( LOWORD( wParam))
{
case IDM_EXIT:
PostMessage( hWnd, WM_CLOSE, 0, 0);
return 0;
}
break;
case WM_GETMINMAXINFO:
((MINMAXINFO *)lParam)->ptMinTrackSize.x = ((MINMAXINFO *)lParam)->ptMaxTrackSize.x = bruttobreite;
((MINMAXINFO *)lParam)->ptMinTrackSize.y = ((MINMAXINFO *)lParam)->ptMaxTrackSize.y = bruttohoehe;
return 0;
case WM_DESTROY:
PostQuitMessage( 0);
return 0;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
int APIENTRY WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR pCmdLine, int nCmdShow)
{
for(ivx = 1; ivx <= anzahltiles; ivx++)
{
if (ivx % 8 == 0)
{
ivy++;
ivx = 1;
}
tilekoord[ivx].x = ivx % 8* 16;
tilekoord[ivx].y = ivy * 16;
}
MSG msg;
HACCEL acc;
WNDCLASSEX wcx;
lol_instance = hInst;
wcx.cbSize = sizeof( wcx);
wcx.lpszClassName = TEXT( "LOL");
wcx.lpfnWndProc = lol_windowhandler;
wcx.style = CS_VREDRAW | CS_HREDRAW;
wcx.hInstance = hInst;
// wcx.hIcon = LoadIcon( hInst, MAKEINTRESOURCE( IDI_MAIN));
// wcx.hIconSm = LoadIcon( hInst, MAKEINTRESOURCE( IDI_MAIN));
wcx.hCursor = LoadCursor( NULL, IDC_ARROW);
wcx.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
// wcx.lpszMenuName = MAKEINTRESOURCE( IDR_MENU);
wcx.cbClsExtra = 0;
wcx.cbWndExtra = 0;
if( !RegisterClassEx( &wcx))
return 0;
// acc = LoadAccelerators( hInst, MAKEINTRESOURCE(IDR_ACCEL));
bruttohoehe = nettohoehe + 2*GetSystemMetrics( SM_CYSIZEFRAME)
+ GetSystemMetrics( SM_CYMENU)
+ GetSystemMetrics( SM_CYCAPTION);
bruttobreite = nettobreite + 2*GetSystemMetrics( SM_CXSIZEFRAME);
lol_window = CreateWindowEx( 0, TEXT( "LOL"), TEXT( "LOL"), WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX,
CW_USEDEFAULT, CW_USEDEFAULT,
bruttobreite, bruttohoehe, NULL, NULL, hInst, NULL);
if( !lol_window)
return 0;
lol_menu = GetMenu( lol_window);
MoveWindow( lol_window, (GetSystemMetrics(SM_CXSCREEN)-bruttobreite)/2,
(GetSystemMetrics(SM_CYSCREEN)-bruttohoehe)/2,
bruttobreite, bruttohoehe, TRUE);
ShowWindow( lol_window, nCmdShow);
while( TRUE)
{
if( PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE))
{
if( GetMessage( &msg, NULL, 0, 0 ) == 0)
return 0;
if( TranslateAccelerator( lol_window, acc, &msg) == 0)
{
TranslateMessage( &msg);
DispatchMessage( &msg);
}
}
else
{
// Hier koennen wir uns um das Spiel kuemmern
}
}
}
class display
{
private:
CDisplay dsply;
CSurface *hgrnd;
CSurface *Tiles;
public:
display()
{
int i;
hgrnd = 0;
Tiles = 0;
}
void free_all()
{
int i;
if (hgrnd)
delete hgrnd;
delete Tiles;
}
~display(){free_all();}
HRESULT init(HWND wnd)
{
HRESULT hr;
int i;
hr = dsply.CreateFullScreenDisplay(wnd, nettobreite, nettohoehe, 16);
if (hr < 0)
return hr;
hr = dsply.CreateSurfaceFromBitmap(&hgrnd, "background.bmp", nettobreite, nettohoehe);
if (hr < 0)
return hr;
hr = dsply.CreateSurfaceFromBitmap(&Tiles, "tileset.bmp", tilehoehe, tilebreite);
if (hr < 0)
return hr;
return S_OK;
}
void draw_background(){dsply.Blt(0,0, hgrnd);}
void draw_Tile(int i, int x, int y)
{
RECT r;
r.left = tilekoord[i].x;
r.top = tilekoord[i].y;
r.right = tilekoord[i].x + 16;
r.bottom = tilekoord[i].y + 16;
dsply.Blt(x, y, Tiles, r);
}
}Allerdings liefert mir der Compiler (Visual Studio 2005) ständig den Fehler "Unerwartetes Dateiende gefunden und verweist auf Zeile 1. Klingt nach richtig argem Noobfehler, aber ich find einfach nichts.
Kann mit bitte jemand helfen?
Angefangen hab ich mal so:
# include <stdio.h>
# include <windows.h>
# include <ddraw.h>
# include <dsound.h>
# include "ddutil.h"
# include "dsutil.h"
# include "resource.h"
class Tile
{
public:
int x;
int y;
Tile(){x = 0; y = 0;};
~Tile(){}
};
const int nettobreite = 640;
const int nettohoehe = 480;
const int anzahltiles = 12;
int bruttobreite;
int bruttohoehe;
int tilehoehe = 32;
int tilebreite = 128;
int ivy = 0;
int ivx;
Tile tilekoord[anzahltiles];
HINSTANCE lol_instance;
HWND lol_window;
HMENU lol_menu;
LRESULT CALLBACK lol_windowhandler( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_COMMAND:
switch( LOWORD( wParam))
{
case IDM_EXIT:
PostMessage( hWnd, WM_CLOSE, 0, 0);
return 0;
}
break;
case WM_GETMINMAXINFO:
((MINMAXINFO *)lParam)->ptMinTrackSize.x = ((MINMAXINFO *)lParam)->ptMaxTrackSize.x = bruttobreite;
((MINMAXINFO *)lParam)->ptMinTrackSize.y = ((MINMAXINFO *)lParam)->ptMaxTrackSize.y = bruttohoehe;
return 0;
case WM_DESTROY:
PostQuitMessage( 0);
return 0;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
int APIENTRY WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR pCmdLine, int nCmdShow)
{
for(ivx = 1; ivx <= anzahltiles; ivx++)
{
if (ivx % 8 == 0)
{
ivy++;
ivx = 1;
}
tilekoord[ivx].x = ivx % 8* 16;
tilekoord[ivx].y = ivy * 16;
}
MSG msg;
HACCEL acc;
WNDCLASSEX wcx;
lol_instance = hInst;
wcx.cbSize = sizeof( wcx);
wcx.lpszClassName = TEXT( "LOL");
wcx.lpfnWndProc = lol_windowhandler;
wcx.style = CS_VREDRAW | CS_HREDRAW;
wcx.hInstance = hInst;
// wcx.hIcon = LoadIcon( hInst, MAKEINTRESOURCE( IDI_MAIN));
// wcx.hIconSm = LoadIcon( hInst, MAKEINTRESOURCE( IDI_MAIN));
wcx.hCursor = LoadCursor( NULL, IDC_ARROW);
wcx.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
// wcx.lpszMenuName = MAKEINTRESOURCE( IDR_MENU);
wcx.cbClsExtra = 0;
wcx.cbWndExtra = 0;
if( !RegisterClassEx( &wcx))
return 0;
// acc = LoadAccelerators( hInst, MAKEINTRESOURCE(IDR_ACCEL));
bruttohoehe = nettohoehe + 2*GetSystemMetrics( SM_CYSIZEFRAME)
+ GetSystemMetrics( SM_CYMENU)
+ GetSystemMetrics( SM_CYCAPTION);
bruttobreite = nettobreite + 2*GetSystemMetrics( SM_CXSIZEFRAME);
lol_window = CreateWindowEx( 0, TEXT( "LOL"), TEXT( "LOL"), WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX,
CW_USEDEFAULT, CW_USEDEFAULT,
bruttobreite, bruttohoehe, NULL, NULL, hInst, NULL);
if( !lol_window)
return 0;
lol_menu = GetMenu( lol_window);
MoveWindow( lol_window, (GetSystemMetrics(SM_CXSCREEN)-bruttobreite)/2,
(GetSystemMetrics(SM_CYSCREEN)-bruttohoehe)/2,
bruttobreite, bruttohoehe, TRUE);
ShowWindow( lol_window, nCmdShow);
while( TRUE)
{
if( PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE))
{
if( GetMessage( &msg, NULL, 0, 0 ) == 0)
return 0;
if( TranslateAccelerator( lol_window, acc, &msg) == 0)
{
TranslateMessage( &msg);
DispatchMessage( &msg);
}
}
else
{
// Hier koennen wir uns um das Spiel kuemmern
}
}
}
class display
{
private:
CDisplay dsply;
CSurface *hgrnd;
CSurface *Tiles;
public:
display()
{
int i;
hgrnd = 0;
Tiles = 0;
}
void free_all()
{
int i;
if (hgrnd)
delete hgrnd;
delete Tiles;
}
~display(){free_all();}
HRESULT init(HWND wnd)
{
HRESULT hr;
int i;
hr = dsply.CreateFullScreenDisplay(wnd, nettobreite, nettohoehe, 16);
if (hr < 0)
return hr;
hr = dsply.CreateSurfaceFromBitmap(&hgrnd, "background.bmp", nettobreite, nettohoehe);
if (hr < 0)
return hr;
hr = dsply.CreateSurfaceFromBitmap(&Tiles, "tileset.bmp", tilehoehe, tilebreite);
if (hr < 0)
return hr;
return S_OK;
}
void draw_background(){dsply.Blt(0,0, hgrnd);}
void draw_Tile(int i, int x, int y)
{
RECT r;
r.left = tilekoord[i].x;
r.top = tilekoord[i].y;
r.right = tilekoord[i].x + 16;
r.bottom = tilekoord[i].y + 16;
dsply.Blt(x, y, Tiles, r);
}
}Allerdings liefert mir der Compiler (Visual Studio 2005) ständig den Fehler "Unerwartetes Dateiende gefunden und verweist auf Zeile 1. Klingt nach richtig argem Noobfehler, aber ich find einfach nichts.
Kann mit bitte jemand helfen?