c++ game developement project.

Pages: 12
closed account (LAfSLyTq)
my teacher told me i need to make a mario type of game on c++ but i dont have any knowledge about makeing a game in c++ past textbased games. any help?

note: im supposed to do it without using an SDK
i can use things suchas DirectX and OpenGL but idk if those are 2d game compatable.
Last edited on
Then there is something wrong with your teacher as DirectX is a SDK. Though you can use other libraries like SDL/SFML and find tutorials for SDL like LazyFoo's series of tutorials.

http://lazyfoo.net/SDL_tutorials/index.php
closed account (LAfSLyTq)
sorry lol, what i meant to say is that she will only allow me to use certain sdk's. sdl is not allowed either =( are there any tutorials?

she also said i could just make the game without an sdk but i think that would be a lot of work.
Wait... you know nothing outside of console development and your teacher expects you to write a jump&run game? I think she's trolling you or something.
Are you sure you understood her properly? Perhaps she meant you couldn't use a complete engine, because making a game and making a SDK are two completely different, unrelated and difficult things to do. Perhaps you should verify the assignment...
closed account (LAfSLyTq)
i just emailed her to ask what she wants me to use in the project she said i have to make the game from scratch. a 2d game, not necessararly playable but she said i just need to make a window that displays an image. i dont know how to make a window.

the actual game has to be finished by the end of july but she said that the rest of the game will come in a later time.
Last edited on
closed account (LAfSLyTq)
ill shorten this, basicly all i need help with right now is making a window appear for my game, and making that windows display an image. any help?
@Invader2010
i need help with right now is making a window appear for my game

You want a window that draws an image?

Here is an example:


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
#include <windows.h>
#include <d2d1.h>
#pragma comment(lib, "d2d1")

#include "basewin.h"

template <class T> void SafeRelease(T **ppT)
{
    if (*ppT)
    {
        (*ppT)->Release();
        *ppT = NULL;
    }
}

class MainWindow : public BaseWindow<MainWindow>
{
    ID2D1Factory            *pFactory;
    ID2D1HwndRenderTarget   *pRenderTarget;
    ID2D1SolidColorBrush    *pBrush;
    D2D1_ELLIPSE            ellipse;

    void    CalculateLayout();
    HRESULT CreateGraphicsResources();
    void    DiscardGraphicsResources();
    void    OnPaint();
    void    Resize();

public:

    MainWindow() : pFactory(NULL), pRenderTarget(NULL), pBrush(NULL)
    {
    }

    PCWSTR  ClassName() const { return L"Circle Window Class"; }
    LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam);
};

// Recalculate drawing layout when the size of the window changes.

void MainWindow::CalculateLayout()
{
    if (pRenderTarget != NULL)
    {
        D2D1_SIZE_F size = pRenderTarget->GetSize();
        const float x = size.width / 2;
        const float y = size.height / 2;
        const float radius = min(x, y);
        ellipse = D2D1::Ellipse(D2D1::Point2F(x, y), radius, radius);
    }
}

HRESULT MainWindow::CreateGraphicsResources()
{
    HRESULT hr = S_OK;
    if (pRenderTarget == NULL)
    {
        RECT rc;
        GetClientRect(m_hwnd, &rc);

        D2D1_SIZE_U size = D2D1::SizeU(rc.right, rc.bottom);

        hr = pFactory->CreateHwndRenderTarget(
            D2D1::RenderTargetProperties(),
            D2D1::HwndRenderTargetProperties(m_hwnd, size),
            &pRenderTarget);

        if (SUCCEEDED(hr))
        {
            const D2D1_COLOR_F color = D2D1::ColorF(1.0f, 1.0f, 0);
            hr = pRenderTarget->CreateSolidColorBrush(color, &pBrush);

            if (SUCCEEDED(hr))
            {
                CalculateLayout();
            }
        }
    }
    return hr;
}

void MainWindow::DiscardGraphicsResources()
{
    SafeRelease(&pRenderTarget);
    SafeRelease(&pBrush);
}

void MainWindow::OnPaint()
{
    HRESULT hr = CreateGraphicsResources();
    if (SUCCEEDED(hr))
    {
        PAINTSTRUCT ps;
        BeginPaint(m_hwnd, &ps);
     
        pRenderTarget->BeginDraw();

        pRenderTarget->Clear( D2D1::ColorF(D2D1::ColorF::SkyBlue) );
        pRenderTarget->FillEllipse(ellipse, pBrush);

        hr = pRenderTarget->EndDraw();
        if (FAILED(hr) || hr == D2DERR_RECREATE_TARGET)
        {
            DiscardGraphicsResources();
        }
        EndPaint(m_hwnd, &ps);
    }
}

void MainWindow::Resize()
{
    if (pRenderTarget != NULL)
    {
        RECT rc;
        GetClientRect(m_hwnd, &rc);

        D2D1_SIZE_U size = D2D1::SizeU(rc.right, rc.bottom);

        pRenderTarget->Resize(size);
        CalculateLayout();
        InvalidateRect(m_hwnd, NULL, FALSE);
    }
}

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR, int nCmdShow)
{
    MainWindow win;

    if (!win.Create(L"Circle", WS_OVERLAPPEDWINDOW))
    {
        return 0;
    }

    ShowWindow(win.Window(), nCmdShow);

    // Run the message loop.

    MSG msg = { };
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}

LRESULT MainWindow::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
    case WM_CREATE:
        if (FAILED(D2D1CreateFactory(
                D2D1_FACTORY_TYPE_SINGLE_THREADED, &pFactory)))
        {
            return -1;  // Fail CreateWindowEx.
        }
        return 0;

    case WM_DESTROY:
        DiscardGraphicsResources();
        SafeRelease(&pFactory);
        PostQuitMessage(0);
        return 0;

    case WM_PAINT:
        OnPaint();
        return 0;


    case WM_SIZE:
        Resize();
        return 0;
    }
    return DefWindowProc(m_hwnd, uMsg, wParam, lParam);
}
Last edited on
OH I'm sorry I forgot a header LOL

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
#pragma once
#include <Windows.h>

template <typename DERIVED_TYPE>
class BaseWindow
{
public:
	static LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
	{
		DERIVED_TYPE* pThis = nullptr;

		if (uMsg == WM_CREATE)
		{
			CREATESTRUCT* pCreate = (CREATESTRUCT*) lParam;
			pThis = (DERIVED_TYPE*)pCreate->lpCreateParams;
			SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)pThis);
			pThis->m_hwnd = hwnd;
		}
		else
		{
			pThis = (DERIVED_TYPE*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
		}

		if (pThis)
		{
			return pThis->HandleMessage(uMsg, wParam, lParam);
		}
		else
		{
			return DefWindowProc(hwnd, uMsg, wParam, lParam);
		}
	}

	BaseWindow() : m_hwnd(nullptr) { }

	BOOL Create(
		PCWSTR lpWindowName,
		DWORD dwStyle,
		DWORD dwExStyle = 0,
		int x = CW_USEDEFAULT,
		int y = CW_USEDEFAULT,
		int nWidth = CW_USEDEFAULT,
		int nHeight = CW_USEDEFAULT,
		HWND  hWndParent = 0,
		HMENU hMenu = 0
		)
	{
		WNDCLASS wc = {0};

		wc.lpfnWndProc = DERIVED_TYPE::WindowProc;
		wc.lpszClassName = ClassName();
		wc.hInstance = GetModuleHandle(nullptr);

		RegisterClass(&wc);

		m_hwnd = CreateWindowEx(
			dwExStyle, ClassName(), lpWindowName, dwStyle, x, y, nWidth, nHeight,
			hWndParent, hMenu, GetModuleHandle(nullptr), this);

		return (m_hwnd ? TRUE : FALSE);
	}

	HWND Window() const { return m_hwnd; }

protected:

	virtual PCWSTR ClassName() const = 0;
	virtual LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) = 0;

	HWND m_hwnd;
};
Don't be intimidated, most WinAPI using applications look like that. If you use something more abstracted, such as SFML, it is MUCH simpler.

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
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
using namespace sf;

int main()
{
    RenderWindow App(VideoMode(1200, 800, 32), "Window Title");

    while (App.IsOpen())
    {
        //poll for window events
        Event event;
        while (App.PollEvent(event))
        {
            if (event.Type==Event::Closed)
            App.Close();
        }

        App.Clear(Color(255,0,0)); //clear window screen to red
        App.Display();

        Sleep(25); //so we dont overload the CPU
    }

    return 0;
}


I just wrote this now, so it may have some syntax errors, but you get the idea.
closed account (LAfSLyTq)
got an error,

Error 1 error C2440: '=' : cannot convert from 'PCWSTR' to 'LPCSTR' c:\users\jeremy\documents\visual studio 2010\projects\section 13\section 13\basewin.h 51 1 Section 13
Error 2 error C2664: 'CreateWindowExA' : cannot convert parameter 2 from 'PCWSTR' to 'LPCSTR' c:\users\jeremy\documents\visual studio 2010\projects\section 13\section 13\basewin.h 58 1 Section 13

it occurs on line 51 and line 58 of the basewin.h file.
any help?

Did you include an 'L' in front of string literals?

1
2
3
4
if (!win.Create(L"Circle", WS_OVERLAPPEDWINDOW))   //that L is supposed to be there
{
    return 0;
}
closed account (LAfSLyTq)
yup.
Can you post the code?
closed account (LAfSLyTq)
its already posted above by codekiddy
I think you need to set an option in visual studio to use multi-byte strings, can't remember where it is exactly, its in the project settings somewhere.
closed account (LAfSLyTq)
nope, actually turns out i already had mine set to multi-byte, i had to change it back to unicode.
closed account (LAfSLyTq)
i would like to thank codekiddy alot for hel[ping me with this but is there any way i could make this window display a .png image?

all i need is for it to display an actual image/sprite that i created and im set.
i said .png just because i prefer .png's
Hmm... actually i'm not sure about that, its something to do with different character representations, the CreateWindowEx function should be #defined to CreateWindowExW iirc, and something in the config needs to be set correctly for this to happen
You can disregard that last post!
Pages: 12