Jump to content

  • Log In with Google      Sign In   
  • Create Account

Maple Wan

Member Since 04 Sep 2013
Offline Last Active Nov 23 2014 11:49 PM

Topics I've Started

Which game engine should I choose?

21 September 2013 - 07:52 PM

Hi:

I'm going to learn game programming. I have the basis of c/c++/c#. I'd like to develop 2D games first and research 3D games later, so which game engine should I choose? There are many many different opensource game engines, so I don't know which is suitable for a beginner. I have learnt some about DirectX, which is not cross-platform, so I may spend more time on OpenGL study. I prefer an opensource game engine whose graphic is based on OpenGL. Give me some suggestions, please. Thanks.


c++ A problem when create a thread in a class

16 September 2013 - 11:54 PM

Dear everyone, I am writing a singleton class to manage a thread, here's the code:

#include <windows.h>

typedef class MsgListener
{
private:
    HANDLE _threadHandle;
    DWORD _threadId;
    static MsgListener *_instance;
    MsgListener();
    DWORD WINAPI Listen(LPVOID param);
public:
    static MsgListener *GetInstance();
    void StartListen();
    void StopListen();
} MSGLISTENER, *LPMSGLISTENER;

MsgListener *MsgListener::_instance = new MsgListener();
MsgListener::MsgListener() 
{ 
    _threadHandle = 0;
    _threadId = 0;
}
DWORD WINAPI MsgListener::Listen(LPVOID param) 
{ 
    // The method to run in a new thread....
}
MsgListener *MsgListener::GetInstance()
{
    if (!_instance)
        _instance = new MsgListener();
    return _instance;
}
void MsgListener::StartListen()
{
    // Here, the compiler give the error:
    // cannot convert 'MsgListener::Listen' from type 'DWORD (MsgListener::)(LPVOID) { aka long unsigned int (MsgListener::)(void*) }' to type 'LPTHREAD_START_ROUTINE { aka long unsigned int (__attribute__((__stdcall__)) *)(void*) }'
    this->_threadHandle = CreateThread(NULL, 0, Listen, 0, &_threadId);
}
void MsgListener::StopListen()
{
    CloseHandle(_threadHandle);
}

The ide I use is Code::Blocks and the compiler is GNU GCC COMPILER.

How to convert the function pointer in a class to the normal function pointer in C? And I really don't know the difference between these two kind of function pointers. Where does each kind of function pointer save in the memory?


DirectX7 SetPalette always failed

04 September 2013 - 10:16 AM

Dear all:

 

I'm learning DirectX, I want to Bind a palette to the PrimarySurface, but the process always failed. I give my code below:

#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
#define SCREEN_BPP 32
#define MAX_COLORS_PALETTE 256

#define DDRAW_INIT_STRUCT(ddstruct) { memset(&ddstruct, 0, sizeof(ddstruct)); ddstruct.dwSize = sizeof(ddstruct); }

LPDIRECTDRAW7 lpdd = NULL;
LPDIRECTDRAWSURFACE7 lpddPrimarySurface = NULL;
LPDIRECTDRAWPALETTE lpddPalette = NULL;
PALETTEENTRY palette[256];

// Omit the unneccessary content

int GameInit()
{
    if (FAILED(DirectDrawCreateEx(NULL, (void**)&lpdd, IID_IDirectDraw7, NULL)))
        return 0;
    if (FAILED(lpdd->SetCooperativeLevel(g_GameHwnd, DDSCL_FULLSCREEN | DDSCL_ALLOWMODEX | DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT)))
        return 0;
    if (FAILED(lpdd->SetDisplayMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, 0, 0)))
        return 0;

    DDRAW_INIT_STRUCT(ddsd);
    ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
    ddsd.dwBackBufferCount = 1;
    ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;

    if (FAILED(lpdd->CreateSurface(&ddsd, &lpddPrimarySurface, NULL)))
        return 0;

    ddsd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER;
    if (FAILED(lpddPrimarySurface->GetAttachedSurface(&ddsd.ddsCaps, &lpddBackSurface)))
        return 0;

    memset(palette, 0, MAX_COLORS_PALETTE * sizeof(PALETTEENTRY));

    for (int index = 0; index < MAX_COLORS_PALETTE; index++)
    {
        if (index < 64)
            palette[index].peRed = index * 4;
        else if (index >= 64 && index < 128)
            palette[index].peGreen = (index - 64) * 4;
        else if (index >= 128 && index < 192)
            palette[index].peBlue = (index - 128) * 4;
        else if (index >= 192 && index < 256)
            palette[index].peRed = palette[index].peGreen = palette[index].peBlue = (index - 192) * 4;

        palette[index].peFlags = PC_NOCOLLAPSE;
    }

    if (FAILED(lpdd->CreatePalette(DDPCAPS_8BIT | DDPCAPS_ALLOW256 | DDPCAPS_INITIALIZE, palette, &lpddPalette, NULL)))
        return 0;

    // I always failed to set palette to primary surface here....
    if (FAILED(lpddPrimarySurface->SetPalette(lpddPalette)))
    {
        MessageBox(NULL, "Failed", NULL, MB_OK);
        return 0;
    }

    DDRAW_INIT_STRUCT(ddsd);

    if (FAILED(lpddBackSurface->Lock(NULL, &ddsd, DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT, NULL)))
        return 0;

    UINT *videoBuffer = (UINT*)ddsd.lpSurface;

    for (int y = 0; y < SCREEN_HEIGHT; y++)
    {
        memset((void*)videoBuffer, y % 256, SCREEN_WIDTH * sizeof(UINT));
        videoBuffer += ddsd.lPitch >> 2;
    }

    if (FAILED(lpddBackSurface->Unlock(NULL)))
        return 0;

 
   return 1;
}

I don't know why I always failed to SetPalette to the primary surface. I set the DisplayMode to 640*480*32, my palette is only 256-color, is this the reason? But I consult the MSDN, CreatePalette can only create 2Bit, 4Bit, 8Bit palettes. Can a 32-bit display mode be compatible with 8Bit palette? Where is the problem?

I feel sorry about that my English is not good. I will be grateful if someone could give me some advice. Thanks.


PARTNERS