Du bist nicht angemeldet.

Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!

Werbeanzeige

T-VIRUS

Alter Hase

  • »T-VIRUS« ist der Autor dieses Themas

Beiträge: 548

Wohnort: Göttingen(West)/Nordhausen(Ost)

Beruf: Schüler

  • Private Nachricht senden

1

07.05.2006, 11:51

Neu in der Spieleprogrammierung!

Hi ich bin neu in diesem Forum und auch neu in der Spieleprogrammierung.

Bis jetzt lerne ich mit dem Buch Game Programming für Dummies aber es befasst sich nur mit den Anfängen und bringt auch leider nicht alles so gut rüber :(
Ich hab aber trotzdem schon etwas kleines zusammen genagelt :P

Hier mal meiner ersten Versuche:

//Main.cpp

C-/C++-Quelltext

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
// INCLUDES ///////////////////////////////////////////////

#define WIN32_LEAN_AND_MEAN  
#define INITGUID

#include <windows.h>   // include important windows stuff

#include <windowsx.h> 
#include <mmsystem.h>
//#include <iostream.h> // include important C/C++ stuff

#include <conio.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <math.h>
#include <io.h>
#include <fcntl.h>  

//Eigene Includes:

#include "Engine.h"

extern CEngine Engine;


HWND main_window_handle = NULL; // save the window handle

HINSTANCE main_instance = NULL; // save the instance

char buffer[80]; 

/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nFunsterStil)

{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = WINDOW_CLASS_NAME;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default color as the background of the window */
    wincl.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           WINDOW_CLASS_NAME,         /* Classname */
           WINDOW_CLASS_NAME,       /* Title Text */
           WS_POPUP,                /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           WINDOW_WIDTH,                 /* The programs width */
           WINDOW_HEIGHT,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nFunsterStil);
    UpdateWindow(hwnd);
    
    // save the window handle and instance in a global

    main_window_handle = hwnd;
    main_instance      = hThisInstance;
    
    //Spiel starten:

    Engine.InitGame();
    

// enter main event loop

while(1)
    {
    if (PeekMessage(&messages,NULL,0,0,PM_REMOVE))
        { 
        // test if this is a quit

        if (messages.message == WM_QUIT)
           break;
    
        // translate any accelerator keys

        TranslateMessage(&messages);

        // send the message to the window proc

        DispatchMessage(&messages);
        } // end if

    
    // main game processing goes here

    Engine.MainGame();

    } // end while

    
    //Hier wird das Spiel herrunter gefahren:

    Engine.ShutdownGame();
    

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}


/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{

        HDC hdc;
        PAINTSTRUCT ps;
        
        
    switch (message)                  /* handle the messages */
    {
        case WM_CREATE:
             break;
              
        case WM_PAINT:
             hdc=BeginPaint(hwnd,&ps);
             
             EndPaint(hwnd, &ps);
             break;
                
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
            
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}


//Engine.h

C-/C++-Quelltext

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
#ifndef Engine_H
#define Engine_H

//Compilierer Includes:

#include <windows.h>              

//Eigene Includes:

#include "DirectX/ddraw.h"

// DEFINES ////////////////////////////////////////////////


// defines for windows 

#define WINDOW_CLASS_NAME "Game.exe"  // class name


#define WINDOW_WIDTH  800              // size of window

#define WINDOW_HEIGHT 600
#define SCREEN_WIDTH  800              // size of screen

#define SCREEN_HEIGHT 600
#define SCREEN_BPP    16               // bits per pixel


// MACROS /////////////////////////////////////////////////


// these read the keyboard asynchronously

#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEY_UP(vk_code)   ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)

// this builds a 16 bit color value in 5.5.5 format (1-bit alpha mode)

#define _RGB16BIT555(r,g,b) ((b & 31) + ((g & 31) << 5) + ((r & 31) << 10))

// this builds a 16 bit color value in 5.6.5 format (green dominate mode)

#define _RGB16BIT565(r,g,b) ((b & 31) + ((g & 63) << 5) + ((r & 31) << 11))

// TYPES //////////////////////////////////////////////////


typedef unsigned short USHORT;
typedef unsigned short WORD;
typedef unsigned char  UCHAR;
typedef unsigned char  BYTE;



class CEngine
{
public:
       
//Konstruktor/Destruktor:

CEngine();
~CEngine();

//Spiel starten:

void InitGame();

//Grafik Stuff:

void InitDirectDraw();
void InitPrimarySurface();
void InitSecondarySurface();

//Tastatur(Eingaben)Stuff:

void InitDirectInput();

//SoundStuff:

void InitDirectSound();

//Hauptteil des Spiels:

void MainGame();


//Spiel beenden:

void ShutdownGame();
        
//Grafik Stuff:

void ReleaseDirectDraw();
void ReleasePrimarySurface();
void ReleaseSecondarySurface();

//Tastatur(Eingaben)Stuff:

void ReleaseDirectInput();

//SoundStuff:

void ReleaseDirectSound();



private:
protected:
};

#endif //Engine_H



//Engine.cpp

C-/C++-Quelltext

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
#include "Engine.h"


LPDIRECTDRAW7        lpdd         = NULL;  // dd object

LPDIRECTDRAWSURFACE7 lpddsprimary = NULL;  // dd primary surface

LPDIRECTDRAWSURFACE7 lpddsback    = NULL;  // dd back surface

LPDIRECTDRAWPALETTE  lpddpal      = NULL;  // a pointer to the created dd palette

DDSURFACEDESC2       ddsd;                 // a direct draw surface description struct

DDSCAPS2             ddscaps;              // a direct draw surface capabilities struct

HRESULT              ddrval;               // result back from dd calls


//Globales Engine Objekt:

CEngine Engine;

//Fenster Hanlder:

extern HWND main_window_handle;
extern HINSTANCE main_instance;
extern char buffer[80];

//Konstruktor/Destruktor:

CEngine::CEngine()
{
}

CEngine::~CEngine()
{
}


//Grafik Stuff:

void CEngine::InitGame()
{
 InitDirectDraw();
 InitPrimarySurface();
 InitSecondarySurface();
 InitDirectSound();
 InitDirectInput();
}

void CEngine::InitDirectDraw()
{
// this function is where you do all the initialization 

// for your game


// create object and test for error

if (DirectDrawCreateEx(NULL, (void **)&lpdd, IID_IDirectDraw7, NULL)!=DD_OK)
{
   MessageBox(main_window_handle, "Das DirectX Objekt konnte nicht erstellt werden!", "Fehler beim erstellen des DX Objekts", MB_OK);
}

// set cooperation level to windowed mode normal

if ((lpdd->SetCooperativeLevel(main_window_handle,
    DDSCL_ALLOWMODEX | DDSCL_FULLSCREEN | 
    DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT))!=DD_OK)
{
    MessageBox(main_window_handle, "Die Kooperationsebene konnte nicht gesetzt werden!", "Kooperationsfehler", MB_OK);
}

// set the display mode

if ((lpdd->SetDisplayMode(SCREEN_WIDTH,SCREEN_HEIGHT,16,0,0))!=DD_OK)
{
   MessageBox(main_window_handle, "Die Bildschrimgröße konnte nicht angepasst werden!", "Display Fehler", MB_OK);
}
}

void CEngine::InitPrimarySurface()
{
// Create the primary surface

memset(&ddsd,0,sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);

// set the flags to validate both the capabilities 

// field and the backbuffer count field

ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;

// we need to let dd know that we want a complex 

// flippable surface structure, set flags for that

ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP |
                      DDSCAPS_COMPLEX;

// set the backbuffer count to 1

ddsd.dwBackBufferCount = 1;

if (lpdd->CreateSurface(&ddsd,&lpddsprimary,NULL)!=DD_OK)
{
 MessageBox(main_window_handle,"Die Primäroberfäche konnte nicht erstellt werden!", "Fehler beim erstellen der Primär Oberfläche",MB_OK);
}


// query for the backbuffer or secondary surface

// notice the use of ddscaps to indicate what 

// we are requesting

ddscaps.dwCaps = DDSCAPS_BACKBUFFER;

// get the surface

lpddsprimary->GetAttachedSurface(&ddscaps,&lpddsback);
}

void CEngine::InitSecondarySurface()
{
}

//Tastatur(Eingaben)Stuff:

void CEngine::InitDirectInput()
{
}

//SoundStuff:

void CEngine::InitDirectSound()
{
}


//Spielteil:

void CEngine::MainGame()
{
// this is the workhorse of your game it will be called

// continuously in real-time this is like main() in C

// all the calls for you game go here!


USHORT *back_buffer = NULL, // used to draw

       *dest_ptr    = NULL, // used in line by line copy

       *src_ptr     = NULL; // " "


// check of user is trying to exit

if (KEY_DOWN(VK_ESCAPE) || KEY_DOWN(VK_SPACE))
    PostMessage(main_window_handle, WM_DESTROY,0,0);

// erase secondary back buffer

memset(&ddsd,0,sizeof(ddsd)); 
ddsd.dwSize = sizeof(ddsd);

// lock the secondary surface

lpddsback->Lock(NULL,&ddsd, 
              DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT,NULL);

// get video pointer to secondary surface

back_buffer = (USHORT *)ddsd.lpSurface;     

// clear back buffer out, remember in 16bit mode there are

// 2 bytes per pixel, so watch out for addressing and multiplying

// issues in this code


// linear memory

if (ddsd.lPitch == SCREEN_WIDTH*2)
    memset(back_buffer,0,SCREEN_WIDTH*SCREEN_HEIGHT*(SCREEN_BPP/8));
else
   {
   // non-linear memory

   
   // make copy of video pointer

   dest_ptr = back_buffer;

   // clear out memory one line at a time

   for (int y=0; y < SCREEN_HEIGHT; y++)
       {
       // clear next line

       memset(dest_ptr,0,SCREEN_WIDTH*(SCREEN_BPP/8));
       
       // advance pointer to next line

       dest_ptr+=(ddsd.lPitch/2);

       } // end for y


   } // end else


// perform game logic...

       
// draw the next frame into the secondary back buffer

// plot 5000 random pixels

for (int index = 0; index < 5000; index++)
    {
    int   x     = rand()%SCREEN_WIDTH;
    int   y     = rand()%SCREEN_HEIGHT;

    UCHAR red   = rand()%256;
    UCHAR green = rand()%256;
    UCHAR blue  = rand()%256;
    
    // back_buffer is a short pointer, BUT lpitch is in bytes

    // so divide by 2, to get number of "pixels" or words

    back_buffer[x+y*(ddsd.lPitch/2)] = _RGB16BIT565(red, green, blue);
    } // end for index


// unlock secondary buffer

lpddsback->Unlock(NULL);

// flip pages

while(lpddsprimary->Flip(NULL, DDFLIP_WAIT)!=DD_OK);

// wait a sec

Sleep(10);

// return success

}




//Grafik Stuff:

void CEngine::ShutdownGame()
{
 ReleaseSecondarySurface();
 ReleasePrimarySurface();
 ReleaseDirectDraw();
}

void CEngine::ReleaseDirectDraw()
{
 // this function is where you shutdown your game and

 // release all resources that you allocated


 // release the directdraw object

 if (lpdd!=NULL)
   lpdd->Release();
}

void CEngine::ReleasePrimarySurface()
{
// first release the primary surface

if (lpddsprimary!=NULL)
   lpddsprimary->Release();
}

void CEngine::ReleaseSecondarySurface()
{
 // first release the secondary surface

 if(lpddsback!=NULL)
   lpddsback->Release();
}

//Tastatur(Eingaben)Stuff:

void CEngine::ReleaseDirectInput()
{
}

//SoundStuff:

void CEngine::ReleaseDirectSound()
{
}



Leider ist der Code zur hälfte noch aud dem Buch aber die Klasse Engine hab ich selbst geschrieben :P
Falls es vorschläge zur verbesserung gibt immer her damit ;)
Meine Blog:)

Wer Bugs im Text findet kann sie melden, fix erscheint irgendwann :D

MFG T-VIRUS

Anonymous

unregistriert

2

07.05.2006, 12:08

- Du hast zuviele Headerdateien die Sinnlos sind.
- Benutzung von extern
- kein Secure Code
- WNDCLASSEX wird nicht richtig initialisiert, ZeroMemory fehlt --> Kann Knallen
- Mach das HWND_DESKTOP weg!!!!!!
- Denglisch: Englisch hier Deutsch da und gemischt mal nicht gemischt, entscheide Dich. Geht sehr auf die Augen
- Benutz einheitliche Kommentierung!
- #define Konstanten weg! Das ist ja grauenvoll
- #define Makros weg! Meine Augen, hilfe
- Sinnlose typedefs weg!
- private, protected und co weg, sind leer: also weg!
- Einrückungen in Deiner Klasse wären mal eine gute Idee
- Globale Variabeln --> Aua
- Flags von SetCooperativeLevel schließen sich auseinander aus --> Undefiniertes Verhalten
- Unsichere C-Casts
- Bei Fehlern läuft das Programm weiter und fährt nicht runter --> Messagebox, danach Systemabsturz - so siehts aus
- Du testest nicht ob der Backbuffer geholt werden konnte!
- Benutz unsigned bei for-Schleifen!
- Du Testest nicht ob die Buffer verriegelt werden konnten, bei Fehlern --> Systemabsturz
- while(lpddsprimary->Flip(NULL, DDFLIP_WAIT)!=DD_OK); << DAS IST WOHL EIN WITZ?!
- mach das Sleep(10) raus, hat keinen Sinn.

Also nimm es mir nicht übel, aber dieses Programm knallt dem User richtig das System ab.

T-VIRUS

Alter Hase

  • »T-VIRUS« ist der Autor dieses Themas

Beiträge: 548

Wohnort: Göttingen(West)/Nordhausen(Ost)

Beruf: Schüler

  • Private Nachricht senden

3

07.05.2006, 12:52

Über die Helfte hat der Programmierer in dem Buch gemacht xD
Sein name ist Andre Lamothe (ka ob den jemand groß kennt ;D)

Ich kann da zwar noch etwas liften aber mehr auch nicht ;D
Okay die DirectX sachen pack ich dann in die Klasse Engine.

Hier mal die Liste der Änderungen:

- Du hast zuviele Headerdateien die Sinnlos sind. V
- Benutzung von extern (Brauch ich für den Fenster Handler!!!)
- kein Secure Code (Kenn mich damit nicht aus!!!)
- WNDCLASSEX wird nicht richtig initialisiert, ZeroMemory fehlt --> Kann Knallen (Was ist das?)
- Mach das HWND_DESKTOP weg!!!!!! V
- Denglisch: Englisch hier Deutsch da und gemischt mal nicht gemischt, entscheide Dich. Geht sehr auf die Augen (Ist leider von Dev-C++ noch das WinAPI stuff(Wird grad entfernt!)
- Benutz einheitliche Kommentierung! V
- #define Konstanten weg! Das ist ja grauenvoll (Warum?)
- #define Makros weg! Meine Augen, hilfe (Wieso?)
- Sinnlose typedefs weg! V
- private, protected und co weg, sind leer: also weg! (Private aufgeüllt und protected weg!)V
- Einrückungen in Deiner Klasse wären mal eine gute Idee
- Globale Variabeln --> Aua V
- Flags von SetCooperativeLevel schließen sich auseinander aus --> Undefiniertes Verhalten
- Unsichere C-Casts
- Bei Fehlern läuft das Programm weiter und fährt nicht runter --> Messagebox, danach Systemabsturz - so siehts aus (Wird per postmessage gefixxt ;)
- Du testest nicht ob der Backbuffer geholt werden konnte! (Werd das auch noch irgendwie einbauen!
- Benutz unsigned bei for-Schleifen! V
- Du Testest nicht ob die Buffer verriegelt werden konnten, bei Fehlern --> Systemabsturz
- while(lpddsprimary->Flip(NULL, DDFLIP_WAIT)!=DD_OK); << DAS IST WOHL EIN WITZ?! (Steht im Buch so!!!
- mach das Sleep(10) raus, hat keinen Sinn. V
Meine Blog:)

Wer Bugs im Text findet kann sie melden, fix erscheint irgendwann :D

MFG T-VIRUS

4

07.05.2006, 14:06

sry hatte jetzt keinen Bock die Fehler für dich zu beseitigen... aber damit das ganze mal an übersichtlichkeit gewinnt:

C-/C++-Quelltext

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
// Engine.h

#ifndef _ENGINE_H_ 
#define _ENGINE_H_

#pragma once

#include <windows.h>              
#include <ddraw.h> 

#define SCREEN_WIDTH  800
#define SCREEN_HEIGHT 600 
#define SCREEN_BPP    16

#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0) 
#define KEY_UP(vk_code)   ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1) 

#define _RGB16BIT555(r,g,b) ((b & 31) + ((g & 31) << 5) + ((r & 31) << 10)) 
#define _RGB16BIT565(r,g,b) ((b & 31) + ((g & 63) << 5) + ((r & 31) << 11)) 

class CEngine 
{ 
public: 
    CEngine(void); 
    ~CEngine(void); 

public:
    bool InitGame(::HWND__* hWnd, ::HINSTANCE__* hInstance); 
    void MainGame(void); 
    void ShutdownGame(void); 

protected:
    bool InitDirectDraw(void); 
    bool InitPrimarySurface(void); 
    bool InitSecondarySurface(void); 
    bool InitDirectInput(void); 
    bool InitDirectSound(void); 
        
public:
    void ReleaseDirectDraw(void); 
    void ReleasePrimarySurface(void); 
    void ReleaseSecondarySurface(void); 
    void ReleaseDirectInput(void); 
    void ReleaseDirectSound(void); 

protected:
    LPDIRECTDRAW7           m_lpDD;
    LPDIRECTDRAWSURFACE7    m_lpDDSPrimary;
    LPDIRECTDRAWSURFACE7    m_lpDDSBack;
    LPDIRECTDRAWPALETTE     m_lpDDPal;
    DDSURFACEDESC2          m_DDSD;
    DDSCAPS2                m_DDScaps; 
    ::HWND__*               m_hWnd;
    ::HINSTANCE__*          m_hInstance;
}; 

#endif


C-/C++-Quelltext

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
// Engine.cpp

#include "Engine.h" 

CEngine::CEngine(void) 
{ 
    m_lpDD         = NULL;
    m_lpDDSPrimary = NULL;
    m_lpDDSBack    = NULL;
    m_lpDDPal      = NULL;
} 

CEngine::~CEngine(void) 
{ 
} 

bool CEngine::InitGame(::HWND__* hWnd, ::HINSTANCE__* hInstance) 
{ 
    if(!hWnd || !hInstance)
        return false;

    m_hWnd      = hWnd;
    m_hInstance = hInstance;

    if(!InitDirectDraw())
        return false;
    if(!InitPrimarySurface())
        return false;
    if(!InitSecondarySurface())
        return false;
    if(!InitDirectSound())
        return false;
    if(!InitDirectInput())
        return false;

    return true;
} 

bool CEngine::InitDirectDraw(void) 
{ 
    if(DirectDrawCreateEx(NULL, (void **)&m_lpDD, IID_IDirectDraw7, NULL) != DD_OK) 
    { 
        ::MessageBox(m_hWnd, "Das DirectX Objekt konnte nicht erstellt werden!", "Fehler beim erstellen des DX Objekts", MB_OK | MB_ICONERROR); 
        return false;
    } 

    if((m_lpDD->SetCooperativeLevel(m_hWnd, DDSCL_ALLOWMODEX | DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT))!=DD_OK) 
    { 
        ::MessageBox(m_hWnd, "Die Kooperationsebene konnte nicht gesetzt werden!", "Kooperationsfehler", MB_OK | MB_ICONERROR); 
        return false;
    } 

    if((m_lpDD->SetDisplayMode(SCREEN_WIDTH, SCREEN_HEIGHT, 16, 0, 0)) != DD_OK) 
    { 
        ::MessageBox(m_hWnd, "Die Bildschrimgröße konnte nicht angepasst werden!", "Display Fehler", MB_OK | MB_ICONERROR); 
        return false;
    } 

    return true;
} 

bool CEngine::InitPrimarySurface(void) 
{ 
    memset(&m_DDSD, 0, sizeof(DDSURFACEDESC2)); 
    m_DDSD.dwSize               = sizeof(DDSURFACEDESC2); 
    m_DDSD.dwFlags              = DDSD_CAPS | DDSD_BACKBUFFERCOUNT; 
    m_DDSD.ddsCaps.dwCaps       = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX; 
    m_DDSD.dwBackBufferCount    = 1; 

    if(m_lpDD->CreateSurface(&m_DDSD, &m_lpDDSPrimary, NULL) != DD_OK) 
    { 
        ::MessageBox(m_hWnd, "Die Primäroberfäche konnte nicht erstellt werden!", "Fehler beim erstellen der Primär Oberfläche", MB_OK | MB_ICONERROR); 
        return false;
    } 
    
    m_DDScaps.dwCaps = DDSCAPS_BACKBUFFER; 

    m_lpDDSPrimary->GetAttachedSurface(&m_DDScaps, &m_lpDDSBack); 

    return true;
} 

bool CEngine::InitSecondarySurface(void) 
{ 
    return true;
} 

bool CEngine::InitDirectInput(void) 
{ 
    return true;
} 

bool CEngine::InitDirectSound(void) 
{ 
    return true;
} 

void CEngine::MainGame(void) 
{ 
    unsigned short *pusBackBuffer   = NULL;
    unsigned short *pusDest         = NULL;
    unsigned short *pusSrc          = NULL;

    if(KEY_DOWN(VK_ESCAPE) || KEY_DOWN(VK_SPACE)) 
        ::PostMessage(m_hWnd, WM_DESTROY, 0, 0); 

    memset(&m_DDSD, 0, sizeof(DDSURFACEDESC2)); 
    m_DDSD.dwSize = sizeof(DDSURFACEDESC2); 

    m_lpDDSBack->Lock(NULL, &m_DDSD, DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT, NULL); 

    pusBackBuffer = (unsigned short*)m_DDSD.lpSurface;      

    if(m_DDSD.lPitch == SCREEN_WIDTH*2) 
        memset(pusBackBuffer, 0, SCREEN_WIDTH*SCREEN_HEIGHT*(SCREEN_BPP/8)); 
    else 
    { 
        pusDest = pusBackBuffer; 
   
        for(unsigned int y = 0; y < (unsigned int)SCREEN_HEIGHT; y++) 
        { 
            memset(pusDest, 0, SCREEN_WIDTH*(SCREEN_BPP/8)); 
            pusDest += (m_DDSD.lPitch/2); 
        }
    }

    for (unsigned int uiIndex = 0; uiIndex < 5000; uiIndex++) 
    { 
        int   x     = rand()%SCREEN_WIDTH; 
        int   y     = rand()%SCREEN_HEIGHT; 

        WORD wRed   = rand()%256; 
        WORD wGreen = rand()%256; 
        WORD wBlue  = rand()%256; 
    
        pusBackBuffer[x+y*(m_DDSD.lPitch/2)] = _RGB16BIT565(wRed, wGreen, wBlue); 
    } 

    m_lpDDSBack->Unlock(NULL); 

    while(m_lpDDSPrimary->Flip(NULL, DDFLIP_WAIT) != DD_OK); 
} 

void CEngine::ShutdownGame(void) 
{ 
    ReleaseSecondarySurface(); 
    ReleasePrimarySurface(); 
    ReleaseDirectDraw(); 
} 

void CEngine::ReleaseDirectDraw(void) 
{ 
    if(m_lpDD) 
    {
        m_lpDD->Release(); 
        m_lpDD = NULL;
    }
} 

void CEngine::ReleasePrimarySurface(void) 
{ 
    if(m_lpDDSPrimary) 
    {
        m_lpDDSPrimary->Release(); 
        m_lpDDSPrimary = NULL;
    }
} 

void CEngine::ReleaseSecondarySurface(void) 
{ 
    if(m_lpDDSBack) 
    {
        m_lpDDSBack->Release(); 
        m_lpDDSBack = NULL;
    }
} 

void CEngine::ReleaseDirectInput(void) 
{ 
} 

void CEngine::ReleaseDirectSound(void) 
{ 
}


C-/C++-Quelltext

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
// Main.cpp

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <mmsystem.h> 
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>

#include "Engine.h" 

#define WINDOW_WIDTH  800
#define WINDOW_HEIGHT 600 

long __stdcall WndProc(::HWND__*, unsigned int, unsigned int, long); 

int __stdcall WinMain (::HINSTANCE__* hInstance, ::HINSTANCE__* hPrevInstance, LPTSTR lpszArgument, int iCmdShow) 

{ 
    ::HWND__*       hWnd;
    ::MSG           msg;
    ::WNDCLASSEX    wincl;

    wincl.hInstance     = hInstance; 
    wincl.lpszClassName = "Engine"; 
    wincl.lpfnWndProc   = WndProc;
    wincl.style         = CS_DBLCLKS;
    wincl.cbSize        = sizeof (WNDCLASSEX); 
    wincl.hIcon         = LoadIcon(NULL, IDI_APPLICATION); 
    wincl.hIconSm       = LoadIcon(NULL, IDI_APPLICATION); 
    wincl.hCursor       = LoadCursor(NULL, IDC_ARROW); 
    wincl.lpszMenuName  = NULL;
    wincl.cbClsExtra    = 0;
    wincl.cbWndExtra    = 0;
    wincl.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); 

    if(!RegisterClassEx(&wincl)) 
        return 0; 

    hWnd = CreateWindowEx(0, "Engine", "Engine Testanwendung 1.0", WS_POPUP, CW_USEDEFAULT, CW_USEDEFAULT, WINDOW_WIDTH, WINDOW_HEIGHT, NULL, NULL, hInstance,  NULL); 

    ShowWindow(hWnd, iCmdShow); 
    UpdateWindow(hWnd); 

    CEngine  Engine;
    Engine.InitGame(hWnd, hInstance); 
    

    while(true)
    {
        if(PeekMessage(&msg,NULL,0,0,PM_REMOVE) == TRUE)
        {
            if(msg.message == WM_QUIT)
                break;

            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        Engine.MainGame();
    }
    Engine.ShutdownGame(); 
    
    return (int)msg.wParam; 
} 

long CALLBACK WndProc(::HWND__* hWnd, unsigned int uiMsg, unsigned int wParam, long lParam) 
{ 
    ::HDC__*        hDC; 
    ::PAINTSTRUCT   ps; 
        
    switch(uiMsg)
    { 
        case WM_CREATE: 
            break; 
        case WM_PAINT: 
             hDC = BeginPaint(hWnd,&ps); 
             EndPaint(hWnd, &ps); 
             break; 
        case WM_DESTROY: 
            PostQuitMessage(0);
            break; 
        default:
            return (long)DefWindowProc(hWnd, uiMsg, wParam, lParam); 
    } 
    return 0; 
}


Aja... wenn de mal das ganze zum Laufen bekommen willst guck dir folgendes einfach mal an:
http://www.germangamedev.de/index.php?site=article&id=9
http://www.germangamedev.de/index.php?site=article&id=10
http://www.germangamedev.de/index.php?site=article&id=11
http://www.germangamedev.de/index.php?site=article&id=12
Devil Entertainment :: Your education is our inspiration
Der Spieleprogrammierer :: Community Magazin
Merlin - A Legend awakes :: You are a dedicated C++ (DirectX) programmer and you have ability to work in a team? Contact us!
Siedler II.5 RttR :: The old settlers-style is comming back!

Also known as (D)Evil

T-VIRUS

Alter Hase

  • »T-VIRUS« ist der Autor dieses Themas

Beiträge: 548

Wohnort: Göttingen(West)/Nordhausen(Ost)

Beruf: Schüler

  • Private Nachricht senden

5

07.05.2006, 14:25

Okay werd mir das alles noch mal durch den Kopf gehen lassen und werd das alles noch optimieren ;)

Die Kapitel 8 und 9 hab ich schon ;)

Aber den rest les ich mir heute abend durch war sehr interessant :) und nix da hat mir auch sehr geholfen ;)

Hab die while schleife entfernt und durch eine if abfrage ausgetauscht.
Soll te was schief gehen gibts aber nur ne fehler meldung also muss ich noch überall postmessage hinpacken ;)
Meine Blog:)

Wer Bugs im Text findet kann sie melden, fix erscheint irgendwann :D

MFG T-VIRUS

T-VIRUS

Alter Hase

  • »T-VIRUS« ist der Autor dieses Themas

Beiträge: 548

Wohnort: Göttingen(West)/Nordhausen(Ost)

Beruf: Schüler

  • Private Nachricht senden

6

08.05.2006, 12:41

Okay heute mittag gibts den bestimmt etwas verbesserten Code des Test Games :)

Wenn ich auch glück hab, dann wird auch 3D Spieleprogrammierung mit C++ und DirectX heute oder Morgen ankommen :)

Hoffe das ich dann einen Besseren und simpleren ein blick in DirectX und Spieleprogrammierung bekomme als mit Game Programming für Dummies was bis jetzt das einzigste der Dummies Bücher ist was mich enttäuscht hat :( :huhu: :wirbel:

EDIT:

Hier die Engine.cpp/.h die ich geändert hab:

Main.cpp

C-/C++-Quelltext

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
// INCLUDES ///////////////////////////////////////////////

#define WIN32_LEAN_AND_MEAN  
#define INITGUID

#include <windows.h>   

/*
#include <windowsx.h> 
#include <mmsystem.h>
#include <iostream.h> 
#include <conio.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <math.h>
#include <io.h>
#include <fcntl.h>  
*/

//Eigene Includes:

#include "Engine.h"

extern CEngine Engine;


HWND main_window_handle = NULL; 
HINSTANCE main_instance = NULL; 
char buffer[80]; 


LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nFunsterStil)

{
    HWND hwnd;               
    MSG messages;            
    WNDCLASSEX wincl;        

    
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = WINDOW_CLASS_NAME;
    wincl.lpfnWndProc = WindowProcedure;      
    wincl.style = CS_DBLCLKS;                 
    wincl.cbSize = sizeof (WNDCLASSEX);

    
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 
    wincl.cbClsExtra = 0;                      
    wincl.cbWndExtra = 0;                      
    
    wincl.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);

    
    if (!RegisterClassEx (&wincl))
        return 0;

    
    hwnd = CreateWindowEx (
           0,                   
           WINDOW_CLASS_NAME,         
           WINDOW_CLASS_NAME,       
           WS_POPUP,                
           CW_USEDEFAULT,       
           CW_USEDEFAULT,       
           WINDOW_WIDTH,                
           WINDOW_HEIGHT,                 
           NULL,        
           NULL,                
           hThisInstance,       
           NULL                 
           );

   
    ShowWindow (hwnd, nFunsterStil);
    UpdateWindow(hwnd);
    
   
    main_window_handle = hwnd;
    main_instance      = hThisInstance;
    
    //Spiel starten:

    Engine.InitGame();
    

// Spielschleife:

while(1)
    {
    if (PeekMessage(&messages,NULL,0,0,PM_REMOVE))
        { 
        
        if (messages.message == WM_QUIT)
           break;
    
        
        TranslateMessage(&messages);

        
        DispatchMessage(&messages);
        } 
    
    //Hier läuft das Spiel ab:

    Engine.MainGame();

    } 
    
    //Hier wird das Spiel herrunter gefahren:

    Engine.ShutdownGame();
    

    
    return messages.wParam;
}




LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{

        HDC hdc;
        PAINTSTRUCT ps;
        
        
    switch (message)                  
    {
        case WM_CREATE:
             break;
              
        case WM_PAINT:
             hdc=BeginPaint(hwnd,&ps);
             
             EndPaint(hwnd, &ps);
             break;
                
        case WM_DESTROY:
            PostQuitMessage (0);       
            break;
            
        default:                      
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}


Engine.h

C-/C++-Quelltext

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
#ifndef Engine_H
#define Engine_H

//Compilierer Includes:

#include <windows.h>              

//Eigene Includes:

#include "DirectX/ddraw.h"

// DEFINES ////////////////////////////////////////////////


// defines for windows 

const char WINDOW_CLASS_NAME[20]="Game";  // class name


const unsigned long WINDOW_WIDTH=800;              // size of window

const unsigned long WINDOW_HEIGHT=600;
const unsigned long SCREEN_WIDTH=800;              // size of screen

const unsigned long SCREEN_HEIGHT=600;
const unsigned long SCREEN_BPP=16;               // bits per pixel


// MACROS /////////////////////////////////////////////////


// these read the keyboard asynchronously

#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEY_UP(vk_code)   ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)

// this builds a 16 bit color value in 5.5.5 format (1-bit alpha mode)

#define _RGB16BIT555(r,g,b) ((b & 31) + ((g & 31) << 5) + ((r & 31) << 10))

// this builds a 16 bit color value in 5.6.5 format (green dominate mode)

#define _RGB16BIT565(r,g,b) ((b & 31) + ((g & 63) << 5) + ((r & 31) << 11))

// TYPES //////////////////////////////////////////////////

/*
typedef unsigned short USHORT;
typedef unsigned short WORD;
typedef unsigned char  UCHAR;
typedef unsigned char  BYTE;
*/


class CEngine
{
 public:
       
 //Konstruktor/Destruktor:

 CEngine();
 ~CEngine();

 //Spiel starten:

 void InitGame();

 //Grafik Stuff:

 void InitDirectDraw();
 void InitPrimarySurface();
 void InitSecondarySurface();

 //Tastatur(Eingaben)Stuff:

 void InitDirectInput();

 //SoundStuff:

 void InitDirectSound();

 //Hauptteil des Spiels:

 void MainGame();


 //Spiel beenden:

 void ShutdownGame();
        
 //Grafik Stuff:

 void ReleaseDirectDraw();
 void ReleasePrimarySurface();
 void ReleaseSecondarySurface();

 //Tastatur(Eingaben)Stuff:

 void ReleaseDirectInput();

 //SoundStuff:

 void ReleaseDirectSound();



 private:
        
 //DirectX Stuff:

 LPDIRECTDRAW7        lpdd;  // dd object

 LPDIRECTDRAWSURFACE7 lpddsprimary;  // dd primary surface

 LPDIRECTDRAWSURFACE7 lpddsback;  // dd back surface

 LPDIRECTDRAWPALETTE  lpddpal;  // a pointer to the created dd palette

 DDSURFACEDESC2       ddsd;                 // a direct draw surface description struct

 DDSCAPS2             ddscaps;              // a direct draw surface capabilities struct

 HRESULT              ddrval;               // result back from dd calls

};

#endif //Engine_H


Engine.cpp

C-/C++-Quelltext

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
#include "Engine.h"

//Globales Engine Objekt:

CEngine Engine;

//Fenster Hanlder:

extern HWND main_window_handle;
extern HINSTANCE main_instance;
extern char buffer[80];

//Konstruktor/Destruktor:

CEngine::CEngine()
{
 lpdd         = NULL;  // dd object

 lpddsprimary = NULL;  // dd primary surface

 lpddsback    = NULL;  // dd back surface

 lpddpal      = NULL;  // a pointer to the created dd palette

}

CEngine::~CEngine()
{
}


//Grafik Stuff:

void CEngine::InitGame()
{
 InitDirectDraw();
 InitPrimarySurface();
 InitSecondarySurface();
 InitDirectSound();
 InitDirectInput();
}

void CEngine::InitDirectDraw()
{
 // this function is where you do all the initialization 

 // for your game


 // create object and test for error

 if (DirectDrawCreateEx(NULL, (void **)&lpdd, IID_IDirectDraw7, NULL)!=DD_OK)
 {
  MessageBox(main_window_handle, "Das DirectX Objekt konnte nicht erstellt werden!", "Fehler beim erstellen des DX Objekts", MB_OK);
 }

 // set cooperation level to windowed mode normal

 if ((lpdd->SetCooperativeLevel(main_window_handle,
     DDSCL_ALLOWMODEX | DDSCL_FULLSCREEN | 
     DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT))!=DD_OK)
 {
    MessageBox(main_window_handle, "Die Kooperationsebene konnte nicht gesetzt werden!", "Kooperationsfehler", MB_OK);
    PostMessage(main_window_handle, WM_DESTROY,0,0);
 }

 // set the display mode

 if ((lpdd->SetDisplayMode(SCREEN_WIDTH,SCREEN_HEIGHT,16,0,0))!=DD_OK)
 {
   MessageBox(main_window_handle, "Die Bildschrimgröße konnte nicht angepasst werden!", "Display Fehler", MB_OK);
   PostMessage(main_window_handle, WM_DESTROY,0,0);
 }
}

void CEngine::InitPrimarySurface()
{
 // Create the primary surface

 memset(&ddsd,0,sizeof(ddsd));
 ddsd.dwSize = sizeof(ddsd);

 // set the flags to validate both the capabilities 

 // field and the backbuffer count field

 ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;

 // we need to let dd know that we want a complex 

 // flippable surface structure, set flags for that

 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP |
                      DDSCAPS_COMPLEX;

 // set the backbuffer count to 1

 ddsd.dwBackBufferCount = 1;

 if (lpdd->CreateSurface(&ddsd,&lpddsprimary,NULL)!=DD_OK)
 {
  MessageBox(main_window_handle,"Die Primäroberfäche konnte nicht erstellt werden!", "Fehler beim erstellen der Primär Oberfläche",MB_OK);
  PostMessage(main_window_handle, WM_DESTROY,0,0);
 }


 // query for the backbuffer or secondary surface

 // notice the use of ddscaps to indicate what 

 // we are requesting

 ddscaps.dwCaps = DDSCAPS_BACKBUFFER;

 // get the surface

 lpddsprimary->GetAttachedSurface(&ddscaps,&lpddsback);
}

void CEngine::InitSecondarySurface()
{
}

//Tastatur(Eingaben)Stuff:

void CEngine::InitDirectInput()
{
}

//SoundStuff:

void CEngine::InitDirectSound()
{
}


//Spielteil:

void CEngine::MainGame()
{
 // this is the workhorse of your game it will be called

 // continuously in real-time this is like main() in C

 // all the calls for you game go here!


 USHORT *back_buffer = NULL, // used to draw

        *dest_ptr    = NULL, // used in line by line copy

        *src_ptr     = NULL; // " "


 // check of user is trying to exit

 if (KEY_DOWN(VK_ESCAPE) || KEY_DOWN(VK_SPACE))
     PostMessage(main_window_handle, WM_DESTROY,0,0);

 // erase secondary back buffer

 memset(&ddsd,0,sizeof(ddsd)); 
 ddsd.dwSize = sizeof(ddsd);

 // lock the secondary surface

 lpddsback->Lock(NULL,&ddsd, 
                 DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT,NULL);

 // get video pointer to secondary surface

 back_buffer = (USHORT *)ddsd.lpSurface;     

 // clear back buffer out, remember in 16bit mode there are

 // 2 bytes per pixel, so watch out for addressing and multiplying

 // issues in this code


 // linear memory

 if (ddsd.lPitch == SCREEN_WIDTH*2)
     memset(back_buffer,0,SCREEN_WIDTH*SCREEN_HEIGHT*(SCREEN_BPP/8));
 else
    {
    // non-linear memory

   
    // make copy of video pointer

    dest_ptr = back_buffer;

    // clear out memory one line at a time

    for (int y=0; y < SCREEN_HEIGHT; y++)
        {
        // clear next line

        memset(dest_ptr,0,SCREEN_WIDTH*(SCREEN_BPP/8));
       
        // advance pointer to next line

        dest_ptr+=(ddsd.lPitch/2);

        } // end for y


    } // end else


 // perform game logic...

       
 // draw the next frame into the secondary back buffer

 // plot 5000 random pixels

 for (unsigned int index = 0; index < 5000; index++)
     {
     int   x     = rand()%SCREEN_WIDTH;
     int   y     = rand()%SCREEN_HEIGHT;

     UCHAR red   = rand()%256;
     UCHAR green = rand()%256;
     UCHAR blue  = rand()%256;
    
     // back_buffer is a short pointer, BUT lpitch is in bytes

     // so divide by 2, to get number of "pixels" or words

     back_buffer[x+y*(ddsd.lPitch/2)] = _RGB16BIT565(red, green, blue);
     } // end for index


 // unlock secondary buffer

 lpddsback->Unlock(NULL);

 // flip pages

 if(lpddsprimary->Flip(NULL, DDFLIP_WAIT)!=DD_OK)
 {
  MessageBox(main_window_handle,"Es gab einen Fehler beim flippen!", "Flipping Fehler!",MB_OK);
  PostMessage(main_window_handle, WM_DESTROY,0,0);
 }
}




//Grafik Stuff:

void CEngine::ShutdownGame()
{
 ReleaseSecondarySurface();
 ReleasePrimarySurface();
 ReleaseDirectDraw();
 ReleaseDirectInput();
 ReleaseDirectSound();
}

void CEngine::ReleaseDirectDraw()
{
 // this function is where you shutdown your game and

 // release all resources that you allocated


 // release the directdraw object

 if (lpdd!=NULL)
   lpdd->Release();
}

void CEngine::ReleasePrimarySurface()
{
 // first release the primary surface

 if (lpddsprimary!=NULL)
     lpddsprimary->Release();
}

void CEngine::ReleaseSecondarySurface()
{
 // first release the secondary surface

 if(lpddsback!=NULL)
    lpddsback->Release();
}

//Tastatur(Eingaben)Stuff:

void CEngine::ReleaseDirectInput()
{
}

//SoundStuff:

void CEngine::ReleaseDirectSound()
{
}


Hoffe die schlimmsten(oder alle) fehler sind jetzt gefixt :)
Wenn nicht wäre wieder eine Check liste a la' nix da sehr nützlich ;)
Meine Blog:)

Wer Bugs im Text findet kann sie melden, fix erscheint irgendwann :D

MFG T-VIRUS

7

08.05.2006, 14:52

- Guck dir mal genau an wie ich dir das gepostet hab... da waren verbesserungen drinne die mir so beim "lesbarmachen" direkt ins Auge gesprungen sind... änder das noch... darunter fällt:
* Füge einen HWND__* als Membervariable hinzu.
* Beseitige nun alle Globalen Variablen(main_window_handle, main_instance, buffer[80]).
* Füge deiner InitGame Funktion einen HWND__* Paramter hinzu.
* Weise der HWND__* Membervariable in der InitGame Function den HWND__* Parameter zu.
=> Globale HWND Variablen sind unnötig.
* Die Init Functionen sollten alle vom Type bool sein, um gegebenen falls ein false zurück liefern zu können... FEHLERBEHANDLUNG!
* Mach das CEngine Objekt aus der Engine Klasse raus... pack das objekt mit in die WinMain ... wird nur in der Function gebraucht -.-
Devil Entertainment :: Your education is our inspiration
Der Spieleprogrammierer :: Community Magazin
Merlin - A Legend awakes :: You are a dedicated C++ (DirectX) programmer and you have ability to work in a team? Contact us!
Siedler II.5 RttR :: The old settlers-style is comming back!

Also known as (D)Evil

T-VIRUS

Alter Hase

  • »T-VIRUS« ist der Autor dieses Themas

Beiträge: 548

Wohnort: Göttingen(West)/Nordhausen(Ost)

Beruf: Schüler

  • Private Nachricht senden

8

08.05.2006, 17:39

Okay werd ich mal machen aber ich möchte nur eins->Bitte schreibt mir keinen Code davon lern ich am ende nichts ;) Ihr sollt mir wenn vorhanden sagen wo fehler sind die ich dann behandeln möchte :)
Meine Blog:)

Wer Bugs im Text findet kann sie melden, fix erscheint irgendwann :D

MFG T-VIRUS

koschka

Community-Fossil

Beiträge: 2 862

Wohnort: Dresden

Beruf: Student

  • Private Nachricht senden

9

08.05.2006, 18:23

Ihr immer mit eurem "bitte schreib mir keinen Code, davon lern ich nix"... Das ist doch großer Quatsch! Gerade durch Code kann man sehr präzise antworten und zeigen wies geht und warum, oft ist dies durch Worte extrem umständlich und einfach nur dumm. Ihr sollt den Code nicht gedankenlos abschreiben ohne euren Kopf anzustengen, sondern anschauen und sagen "aha", der macht das so und so, warum eigentlich? Was zum Teufel sind ... Wie funktioniert das ganz genau. Wenn ihr euch genau diese Fragen stellt, macht ihr es richtig, dann heisst es Selbststudium mit google, msdn & co. Wenn ihr mal nicht weiterwisst, könnt ihr natürlich auch Fragen.

Ein Programmierer muss nicht etliches aus dem Kopf wissen (mit welchen Funktionen man z.B. Strings manipuliert, sondern sich schnell und exakt in die Materie einarbeiten und ein Problem lösen können.

T-VIRUS

Alter Hase

  • »T-VIRUS« ist der Autor dieses Themas

Beiträge: 548

Wohnort: Göttingen(West)/Nordhausen(Ost)

Beruf: Schüler

  • Private Nachricht senden

10

08.05.2006, 18:28

Okay stimmt :) aber ich sollte zumindestens diese Fehler selbst lösen können ;) falls es Probleme gibt sag ich natürlich bescheide ;)

Aber warum schreibst man:

C-/C++-Quelltext

1
2
3
4
5
6
    ::HWND__*
    ::HINSTANCE__* 

    //Und nicht einfach:

   HWND
   HINSTANCE


Hab einen Fehler gehabt und musste den Code Back Upen :(
Leider stürtzte das Spiel ab nachdem ich das erste mal durch die Game schleife war :( Aber das als ich die Member HWND, HINSTANCE über eine Initalisierungsmethode initalisieren wollte :(

Ich werd mal gucken wie ich das ändern kann ;)
Meine Blog:)

Wer Bugs im Text findet kann sie melden, fix erscheint irgendwann :D

MFG T-VIRUS

Werbeanzeige