c++ vector problem - c++

i have to event that should insert values in two vectors but when event get called for the first time it inserts the values successfully but when it get called again it will insert the values in the first vector but not in the second vector than when another event get called the second vector's values will be deleted.
here is the full code
#define MAX_LOADSTRING 100
RECT *rect;
const UINT_PTR EVERYTHING_ID=0x1;
const UINT_PTR LBUTTONDOWN_ID=0x3;
const UINT_PTR TDENEMIE1_ID=0x4;
const UINT_PTR TAENEMIE1_ID=0x5;
int conno=2;
int side=0;
int cEnemie1=0;
int dEnemie1=1;
int aEnemie1=0;
int sEnemie1=1;
bool e1=true;
time_t now;
time_t tEnemie1;
vector <POINT> vRegularShots;
vector <POINT> vS1Enemie1;
vector <POINT> vS2Enemie1;
VOID Paint(HDC hdc, HWND hWnd)
{
hdc=GetDC(hWnd);
HDC memDC=CreateCompatibleDC(hdc);
HBITMAP hMemMap=CreateCompatibleBitmap(hdc, 225, 350);
HBITMAP hOldMap=(HBITMAP)SelectObject(memDC, hMemMap);
Graphics draw(memDC);
// Drawing
Image bg(L"bg.jpg");
draw.DrawImage(&bg, 0, 0);
// Regular shots
Image shot(L"RegularShots.png");
long s=vRegularShots.size();
// Draw shots
for(long index=0; index < (long)vRegularShots.size(); ++index)
{
draw.DrawImage(&shot, vRegularShots[index].x, vRegularShots[index].y);
}
// Update the shots
for(long index=0; index < (long)vRegularShots.size(); ++index)
{
vRegularShots[index].y--;
}
// Create Enemies
if(dEnemie1==0)
{
if(cEnemie1<2)
{
if(aEnemie1==0)
{
SetTimer(hWnd, TAENEMIE1_ID, 550, NULL);
}
aEnemie1=1;
cEnemie1++;
}
else
{
KillTimer(hWnd, TDENEMIE1_ID);
}
dEnemie1=1;
}
// Draw enemies
for(long index=0; index < (long)vS1Enemie1.size(); ++index)
{
Image iEnemie1(L"Enemie1.png");
draw.DrawImage(&iEnemie1, vS1Enemie1[index].x, vS1Enemie1[index].y);
}
for(long index=0; index < (long)vS2Enemie1.size(); ++index)
{
Image iEnemie1(L"Enemie1.png");
draw.DrawImage(&iEnemie1, vS2Enemie1[index].x, vS2Enemie1[index].y);
}
// Update enemies
for(long index=0; index < (long)vS1Enemie1.size(); index++)
{
vS1Enemie1[index].x++;
vS1Enemie1[index].y++;
}
for(long index=0; index < (long)vS2Enemie1.size(); index++)
{
vS2Enemie1[index].x--;
vS2Enemie1[index].y++;
}
// Delete enemies
for(long index=0; index < (long)vS1Enemie1.size(); index++)
{
if(vS1Enemie1[index].x>225)
{
vS1Enemie1.erase(vS1Enemie1.begin()+index);
}
}
for(long index=0; index < (long)vS2Enemie1.size(); index++)
{
if(vS2Enemie1[index].x>225)
{
vS2Enemie1.erase(vS2Enemie1.begin()+index);
}
}
BitBlt(hdc, 0, 0, 225, 350, memDC, 0, 0, SRCCOPY);
ReleaseDC(hWnd, hdc);
SelectObject(memDC, hOldMap);
DeleteObject(hMemMap);
DeleteDC(memDC);
}
VOID CheckDead()
{
for(long index=0; index < (long)vRegularShots.size(); ++index)
{
vRegularShots[index].x, vRegularShots[index].y;
}
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
POINT pt;
POINT pts;
switch (message)
{
case WM_CREATE:
SetTimer(hWnd, EVERYTHING_ID, 1, NULL);
break;
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
Paint(hdc, hWnd);
EndPaint(hWnd, &ps);
break;
case WM_LBUTTONDOWN:
GetCursorPos(&pt);
ScreenToClient(hWnd, &pt);
if(conno==1)
{
conno++;
}
else
{
pt.x+=18;
conno--;
}
vRegularShots.push_back(pt);
SetTimer(hWnd, LBUTTONDOWN_ID, 350, NULL);
InvalidateRect(hWnd, rect, false);
break;
case WM_LBUTTONUP:
KillTimer(hWnd, LBUTTONDOWN_ID);
break;
case WM_TIMER:
switch(wParam)
{
case EVERYTHING_ID:
if(e1==true)
{
now=time(NULL);
tEnemie1=now+1;
e1=false;
}
now=time(NULL);
if(now==tEnemie1)
{
SetTimer(hWnd, TDENEMIE1_ID, 550, NULL);
}
InvalidateRect(hWnd, rect, false);
break;
case LBUTTONDOWN_ID:
GetCursorPos(&pt);
ScreenToClient(hWnd, &pt);
if(conno==1)
{
conno++;
}
else
{
pt.x+=18;
conno--;
}
vRegularShots.push_back(pt);
break;
case TDENEMIE1_ID:
pt.y=5;
pt.x=-26;
vS1Enemie1.push_back(pt);
pt.y=52;
pt.x=251;
vS2Enemie1.push_back(pt);
dEnemie1=0;
InvalidateRect(hWnd, rect, false);
break;
case TAENEMIE1_ID:
InvalidateRect(hWnd, rect, false);
break;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
i set a break point at dEnemie1=0 in TAENEMIE1_ID.
anyidea?

For one thing, these two loops have issues:
// Delete enemies
for(long index=0; index < (long)vS1Enemie1.size(); index++)
{
if(vS1Enemie1[index].x>225)
{
vS1Enemie1.erase(vS1Enemie1.begin()+index);
}
}
for(long index=0; index < (long)vS2Enemie1.size(); index++)
{
if(vS2Enemie1[index].x>225)
{
vS2Enemie1.erase(vS2Enemie1.begin()+index);
}
}
One issue with each is that when you erase the POINT at index index, then all POINTs at indices index + 1, index + 2, etc. are shifted to the left by one. So, when you increment index, you skip a POINT. The other issue is that you should be using the unsigned size_type as the type of your indexing variable rather than casting to long, which is signed.
Here is corrected code:
// Delete enemies
vector <POINT>::size_type index;
for(index=0; index < vS1Enemie1.size(); )
{
if(vS1Enemie1[index].x>225)
{
vS1Enemie1.erase(vS1Enemie1.begin()+index);
}
else
++index;
}
for(index=0; index < vS2Enemie1.size(); )
{
if(vS2Enemie1[index].x>225)
{
vS2Enemie1.erase(vS2Enemie1.begin()+index);
}
else
++index;
}
It would be even better if you used iterators:
// Delete enemies
vector <POINT>::iterator it;
for(it=vS1Enemie1.begin(); it != vS1Enemie1.end(); )
{
if(it->x > 225)
{
it = vS1Enemie1.erase(it);
}
else
++it;
}
for(it=vS2Enemie1.begin(); it != vS2Enemie1.end(); )
{
if(it->x > 225)
{
it = vS2Enemie1.erase(it);
}
else
++it;
}

Related

C++ WINAPI How can I run an event of mouse only once?

This code in my windowless framework is an area like just button,but some code I wanted to run it only once until the mouse move on the area next time.
All those code are in WM_MOUSEMOVE.
int ClickAreaPtInfo(HWND hWnd, int x, int y, int sizex, int sizey, LPARAM lParam,int &value)
{
POINT pt;
pt.x = LOWORD(lParam);
pt.y = HIWORD(lParam);
RECT rect;
GetClientRect(hWnd, &rect);
RECT rc = { x,y,x + sizex,y + sizey };
if (PtInRect(&rc, pt))
{
value = 1;
return -1;
}
else
{
value = 0;
return -1;
}
return -1;
}
int _CreateMouseEvent(HWND hWnd, int x, int y, int sizex, int sizey, LPARAM lParam,RUNFUN function(), const wchar_t* Panelid, const wchar_t* CtlName)
{
int val = 0;
int msg = 0;
//int ist = 0; int istprev = 0;
RECT winrc;
GetClientRect(hWnd, &winrc);
RECT rc;
RectTypeConvert(rc,x, y, sizex, sizey);
if (Panelid == PanelID)
{
int nst = 1;
//OutputDebugString(L"HOVER!\n");
msg = 1;
ClickAreaPtInfo(hWnd, x, y, sizex, sizey, lParam, val);
if (val == 1)
{
if (ClickMsg == 1) //Click(Get from WM_LBUTTONUP)
{
ClickMsg = 0;
function();
}
else
{
//It must be run for only once until the mouse leave next time,or it will lead to a lot of resource occupation
if (CtlName == L"Button") //HOVER
{
if (nst == 1)
{
HDC hdc = GetDC(hWnd);
CreateSimpleButtonEx(hWnd, hdc, x, y, sizex, sizey, UICOLOR_GREENSEA, 1, ButtonText);
ReleaseDC(hWnd, hdc);
nst = 0;
return 0;
}
else
{
nst = 0;
return 0;
}
return 0;
}
if (CtlName == L"CloseButton") ///HOVER
{
if (nst == 1)
{
HDC hdc = GetDC(hWnd);
CreateRect(hWnd, hdc, x, y, sizex, sizey, UICOLOR_PEACHRED);
PanelDrawCloseBtn(hWnd, hdc, rc.right - 40, 0, 40, 40, 12, UICOLOR_WHITE);
ReleaseDC(hWnd, hdc);
nst = 0;
return 0;
}
else
{
nst = 0;
return 0;
}
return 0;
}
else
{
return 0;
}
}
}
if (val == 0) //Leave
{
nst = 1;
InvalidateRect(hWnd, &rc, 0); //It must be run for only once until the mouse leave next time,or it will lead to a lot of resource(CPU) occupation
}
}
if (Panelid == PrevPanelID)
{
msg = 1;
}
else
{
msg = 0;
}
return 0;
}
then handle CreateMouseEvent in WM_MOUSEMOVE:
case WM_MOUSEMOVE:
{
CreateMouseEvent(hWnd, 20, 60, 140, 40, lParam, test, L"Init",L"Button");
CreateMouseEvent(hWnd, 20, 120, 140, 40, lParam, test3, L"Init", L"Button");
CreateMouseEvent(hWnd, 20, 180, 140, 40, lParam, btn3, L"Init",L"Button");
CreateMouseEvent(hWnd, rc.right - 40, 0, 40, 40, lParam, CloseWindow, L"Init",L"CloseButton");
break;
}
And I will also give a picture to this question!
http://db.vertexstudio.xyz/lnk/PanelPic/debuginf.png
any solution?thanks!
Some of your problems have already been mentioned above.
There are some variables that are not defined in your example, neither type nor value, but I can extrapolate some at least.
Also, in several places, you try to directly compare a C style string pointer (not a std::string!) to another C style string pointer.
if (Panelid == PanelID) is one place
if (CtlName == L"Button") //HOVER === is another
if (CtlName == L"CloseButton") ///HOVER another
You can use something like int _wcsnicmp( const wchar_t *string1,const wchar_t *string2, size_t count); or whatever your compiler supports. Optionally, you could just use std::wstring.
As for the click, you need to save the rect you get with GetClientRect(hWnd, &winrc); It needs persistence, ie saved outside of the function and accessible. Then compare to this rectangle and act accordingly.

Cannot open include file: 'basewin.h': No such file or directory

I'm trying to figure out how I can find where to either download the file set or get rid of this error. This elusive 'basewin.h' is nowhere to be found. So I don't know if I'm missing a library or what. I've also tried switching around the 'windows.h' and 'basewin.h' statements. I've tried disabling precompiled headers. Apparently this file holds the information for the "basewindow" class.
I'm using visual studio 2013 and I'm using a win32 project.I'm trying to run the example from the microsoft tutorial website http://msdn.microsoft.com/en-us/library/windows/desktop/ff684181(v=vs.85).aspx. Any help would be very much appreciated. Here is the code:
#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);
}
The page you linked says: "The program re-uses the BaseWindow class that was defined in the topic Managing Application State".
That link contains the code for the BaseWindow class, which you should put in basewin.h.

empty std::list throwing exception when destructor is called

So if I use the code below when the program exits I get an exception in std::list at this location. This is regardless of whether I add items to it or not.
void clear() _NOEXCEPT
{ // erase all
#if _ITERATOR_DEBUG_LEVEL == 2
this->_Orphan_ptr(*this, nullptr);
#endif /* _ITERATOR_DEBUG_LEVEL == 2 */
_Nodeptr _Pnode = this->_Nextnode(this->_Myhead);
this->_Nextnode(this->_Myhead) = this->_Myhead;
this->_Prevnode(this->_Myhead) = this->_Myhead;
this->_Mysize = 0;
for (_Nodeptr _Pnext; _Pnode != this->_Myhead; _Pnode = _Pnext)
{ // delete an element
_Pnext = this->_Nextnode(_Pnode); //Unhandled exception, access violation
this->_Freenode(_Pnode);
}
}
Here's the only container I'm using std::list in currently and I only have 1 of them ...
if I switch std::list to std::vector then it works fine.
#pragma once
#ifndef EVENT_H
#define EVENT_H
#include <delegate.h>
#include <list>
#include <vector>
using fd::delegate;
template<typename... Args>
class Event;
template<typename...Args>
class Event<void(Args...)>
{
private:
std::list<delegate<void(Args...)>> m_delegates;
public:
void addListener(const delegate<void(Args...)>& d)
{
if (d)
m_delegates.push_back(d);
}
void removeListener(const delegate<void(Args...)>& d)
{
if (d)
m_delegates.remove(d);
}
void removeDeadListeners()
{
for (auto i = m_delegates.begin(); i != m_delegates.end(); i++)
{
if (i->empty())
m_delegates.remove(i);
}
}
void operator()(Args... args)
{
for (auto& d : m_delegates)
{
d(args...);
}
}
};
#endif //EVENT_H
Any way to use std::list?
I believe your problem is that you're removing items from the list while iterating over it, but I'm surprised the code you've shown compiles at all.
for (auto i = m_delegates.begin(); i != m_delegates.end(); i++)
{
if (i->empty())
m_delegates.remove(i); // <-- This shouldn't compile
}
list::remove takes a list::value_type as the argument, and not list::iterator, so the above should fail to compile. The solution is to use list::erase
for (auto i = m_delegates.begin(); i != m_delegates.end(); )
{
if (i->empty())
i = m_delegates.erase(i); // get iterator to next element
else
++i;
}
You could also eliminate the loop completely and use list::remove_if
m_delegates.remove_if([](decltype(m_delegates)::value_type const& elem) {
return elem.empty();
});
I've determined it's just something to do with the class it is declared in as I can use Event fine in other classes ..... I'm just confused because I NEVER call a single method. I'm just modifying a tutorial code to make it a bit easier to work with for a test project of mine. The default constructor works fine for other classes and I can add / call methods fine it's just this one ... I'm stumped.
The .h
#pragma once
#ifndef D3DAPP_H
#define D3DAPP_H
#pragma warning(push)
#pragma warning( disable : 4005) //Ignore Identical Macro declarations that exist in Windows and DirectX headers
#include <D3D11.h>
#include <D3DX11.h>
#include <Windows.h>
#pragma warning(pop)
#include "GameTimer.h"
#include <string>
#include "Event.h"
class D3DApp
{
public:
D3DApp(HINSTANCE hInstance);
virtual ~D3DApp();
HINSTANCE AppInst() const;
HWND MainWnd() const;
float AspectRatio() const;
int Run();
/*
Framework methods. Derived client class overrides these methods
to implement specific application requirements
*/
virtual bool Init();
virtual void Release();
Event<void(int,int)> OnResize;
virtual void UpdateScene(float dt) = 0;
virtual void DrawScene() = 0;
virtual LRESULT MsgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
//Convenience overrides for handling mouse input
virtual void OnMouseDown(WPARAM btnState, int x, int y);
virtual void OnMouseUp(WPARAM btnState, int x, int y);
virtual void OnMouseMove(WPARAM btnState, int x, int y);
protected:
virtual void ApplicationResize(int w, int h);
bool InitMainWindow();
bool InitDirect3D();
void CalculateFrameStats();
protected:
HINSTANCE mAppInstance;
HWND mMainWnd;
bool mAppPaused;
bool mMinimized;
bool mMaximized;
bool mResizing;
bool mInitialized;
UINT m4xMsaaQuality;
GameTimer mTimer;
ID3D11Device* md3dDevice;
ID3D11DeviceContext* md3dImmediateContext;
IDXGISwapChain* mSwapChain;
ID3D11Texture2D* mDepthStencilBuffer;
ID3D11RenderTargetView* mRenderTargetView;
ID3D11DepthStencilView* mDepthStencilView;
D3D11_VIEWPORT mScreenViewport;
std::wstring mMainWndCaption;
D3D_DRIVER_TYPE md3dDriverType;
int mClientWidth;
int mClientHeight;
bool mEnable4xMsaa;
};
#endif //D3DAPP_H
The .cpp
#include "d3dApp.h"
#include "Utility.h"
#include <assert.h>
#include <sstream>
D3DApp* g_d3dApp = 0;
LRESULT CALLBACK
MainWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
return g_d3dApp->MsgProc(hwnd, msg, wParam,lParam);
}
D3DApp::D3DApp(HINSTANCE hInstance)
: mAppInstance(hInstance),
mMainWndCaption(L"D3D11 Application"),
md3dDriverType(D3D_DRIVER_TYPE_HARDWARE),
mClientWidth(800),
mClientHeight(600),
mEnable4xMsaa(false),
mMainWnd(0),
mAppPaused(false),
mMinimized(false),
mMaximized(false),
mResizing(false),
mInitialized(false),
m4xMsaaQuality(0),
md3dDevice(0),
md3dImmediateContext(0),
mSwapChain(0),
mDepthStencilBuffer(0),
mRenderTargetView(0),
mDepthStencilView(0)
{
ZeroMemory(&mScreenViewport, sizeof(D3D11_VIEWPORT));
g_d3dApp = this;
}
void D3DApp::Release()
{
ReleaseCOM(mRenderTargetView);
ReleaseCOM(mDepthStencilView);
ReleaseCOM(mSwapChain);
ReleaseCOM(mDepthStencilBuffer);
if (md3dImmediateContext)
md3dImmediateContext->ClearState();
ReleaseCOM(md3dImmediateContext);
ReleaseCOM(md3dDevice);
if (g_d3dApp == this)
g_d3dApp = nullptr;
mInitialized = false;
}
D3DApp::~D3DApp()
{
if (mInitialized) Release();
}
float D3DApp::AspectRatio() const
{
return static_cast<float>(mClientWidth) / mClientHeight;
}
int D3DApp::Run()
{
MSG msg = { 0 };
mTimer.Reset();
while (msg.message != WM_QUIT)
{
if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
mTimer.Tick();
CalculateFrameStats();
UpdateScene(mTimer.DeltaTime());
DrawScene();
}
}
return (int)msg.wParam;
}
bool D3DApp::Init()
{
if (!InitMainWindow())
return false;
if (!InitDirect3D())
return false;
mInitialized = true;
return true;
}
void D3DApp::ApplicationResize(int w,int h)
{
assert(md3dImmediateContext);
assert(md3dDevice);
assert(mSwapChain);
ReleaseCOM(mRenderTargetView);
ReleaseCOM(mDepthStencilView);
ReleaseCOM(mDepthStencilBuffer);
mClientWidth = w;
mClientHeight = h;
HR(mSwapChain->ResizeBuffers(1, mClientWidth, mClientHeight, DXGI_FORMAT_R8G8B8A8_UNORM, 0));
ID3D11Texture2D* backBuffer;
HR(mSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), reinterpret_cast<void**>(&backBuffer)));
HR(md3dDevice->CreateRenderTargetView(backBuffer, 0, &mRenderTargetView));
ReleaseCOM(backBuffer);
D3D11_TEXTURE2D_DESC depthStencilDesc;
ZeroMemory(&depthStencilDesc, sizeof(D3D11_TEXTURE2D_DESC));
depthStencilDesc.Width = mClientWidth;
depthStencilDesc.Height = mClientHeight;
depthStencilDesc.MipLevels = 1;
depthStencilDesc.ArraySize = 1;
depthStencilDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
if (mEnable4xMsaa)
{
depthStencilDesc.SampleDesc.Count = 4;
depthStencilDesc.SampleDesc.Quality = m4xMsaaQuality - 1;
} //No MSAA
else
{
depthStencilDesc.SampleDesc.Count = 1;
depthStencilDesc.SampleDesc.Quality = 0;
}
depthStencilDesc.Usage = D3D11_USAGE_DEFAULT;
depthStencilDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
depthStencilDesc.CPUAccessFlags = 0;
depthStencilDesc.MipLevels = 0;
HR(md3dDevice->CreateTexture2D(&depthStencilDesc, 0, &mDepthStencilBuffer));
HR(md3dDevice->CreateDepthStencilView(mDepthStencilBuffer, 0, &mDepthStencilView));
md3dImmediateContext->OMSetRenderTargets(1, &mRenderTargetView,mDepthStencilView);
mScreenViewport.TopLeftX = 0;
mScreenViewport.TopLeftY = 0;
mScreenViewport.Width = static_cast<float>(mClientWidth);
mScreenViewport.Height = static_cast<float>(mClientWidth);
mScreenViewport.MinDepth = 0.0f;
mScreenViewport.MaxDepth = 1.0f;
md3dImmediateContext->RSSetViewports(1, &mScreenViewport);
//OnResize(w, h);
}
LRESULT D3DApp::MsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
//Ignore messages until we have intialized everything
if (!mInitialized)
return DefWindowProc(hWnd, msg, wParam, lParam);
switch (msg)
{
case WM_ACTIVATE:
if (LOWORD(wParam) == WA_INACTIVE)
{
mAppPaused = true;
mTimer.Stop();
}
else
{
mAppPaused = false;
mTimer.Start();
}
return 0;
case WM_SIZE:
if (wParam == SIZE_MINIMIZED)
{
mAppPaused = true;
mMinimized = true;
mMaximized = false;
mTimer.Stop();
}
else if (wParam == SIZE_MAXIMIZED)
{
mAppPaused = SIZE_MAXIMIZED;
mMinimized = false;
mMaximized = true;
ApplicationResize(LOWORD(lParam), HIWORD(lParam));
}
else if (wParam == SIZE_RESTORED)
{
if (mMinimized)
{
mAppPaused = false;
mMinimized = false;
mTimer.Start();
ApplicationResize(LOWORD(lParam), HIWORD(lParam));
}
else if (mMaximized)
{
mAppPaused = false;
mMaximized = false;
ApplicationResize(LOWORD(lParam), HIWORD(lParam));
}
else if (mResizing)
{
//We don't want to do stuff while this is going on
}
else
{
ApplicationResize(LOWORD(lParam), HIWORD(lParam));
}
}
return 0;
case WM_ENTERSIZEMOVE:
mAppPaused = true;
mResizing = true;
mTimer.Stop();
return 0;
case WM_EXITSIZEMOVE:
mAppPaused = false;
mResizing = false;
mTimer.Start();
if (lParam != 0)
ApplicationResize(LOWORD(lParam), HIWORD(lParam));
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_MENUCHAR:
//Don't beep when we alt-enter
return MAKELRESULT(0, MNC_CLOSE);
case WM_GETMINMAXINFO:
((MINMAXINFO*)lParam)->ptMinTrackSize.x = 200;
((MINMAXINFO*)lParam)->ptMinTrackSize.y = 200;
return 0;
case WM_LBUTTONDOWN:
case WM_MBUTTONDOWN:
case WM_RBUTTONDOWN:
OnMouseDown(wParam, LOWORD(lParam), HIWORD(lParam));
return 0;
case WM_LBUTTONUP:
case WM_MBUTTONUP:
case WM_RBUTTONUP:
OnMouseUp(wParam, LOWORD(lParam), HIWORD(lParam));
return 0;
case WM_MOUSEMOVE:
OnMouseMove(wParam, LOWORD(lParam), HIWORD(lParam));
return 0;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
bool D3DApp::InitMainWindow()
{
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = MainWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = mAppInstance;
wc.hIcon = LoadIcon(0, IDI_APPLICATION);
wc.hCursor = LoadCursor(0, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
wc.lpszMenuName = 0;
wc.lpszClassName = L"D3DWndClassName";
if (!RegisterClass(&wc))
{
MessageBox(0, L"RegisterClass Failed!", 0, 0);
return false;
}
RECT R = { 0, 0, mClientWidth, mClientHeight };
AdjustWindowRect(&R, WS_OVERLAPPEDWINDOW, false);
int width = R.right - R.left;
int height = R.bottom - R.top;
mMainWnd = CreateWindow(L"D3DWndClassName", mMainWndCaption.c_str(),
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, width, height, 0, 0, mAppInstance, 0);
if (mMainWnd);
{
MessageBox(0, L"CreateWindow Failed", 0, 0);
return false;
}
ShowWindow(mMainWnd, SW_SHOW);
UpdateWindow(mMainWnd);
return true;
}
bool D3DApp::InitDirect3D()
{
//Create the device and device context
UINT createDeviceFlags = 0;
#if defined(DEBUG) || defined(_DEBUG)
createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif
D3D_FEATURE_LEVEL featureLevel;
HRESULT hr = D3D11CreateDevice(
0,
md3dDriverType,
0,
createDeviceFlags,
0, 0,
D3D11_SDK_VERSION,
&md3dDevice,
&featureLevel,
&md3dImmediateContext);
if (FAILED(hr))
{
MessageBox(0, L"D3D11CreateDevice Failed.", 0, 0);
return false;
}
if (featureLevel != D3D_FEATURE_LEVEL_11_0)
{
MessageBox(0, L"Direct3D Feature Level 11 unsupported", 0, 0);
return false;
}
HR(md3dDevice->CheckMultisampleQualityLevels(
DXGI_FORMAT_R8G8B8A8_UNORM, 4, &m4xMsaaQuality));
assert(m4xMsaaQuality > 0);
DXGI_SWAP_CHAIN_DESC sd;
sd.BufferDesc.Width = mClientWidth;
sd.BufferDesc.Height = mClientHeight;
sd.BufferDesc.RefreshRate.Numerator = 60;
sd.BufferDesc.RefreshRate.Denominator = 1;
sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
sd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
sd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
if (mEnable4xMsaa)
{
sd.SampleDesc.Count = 4;
sd.SampleDesc.Quality = m4xMsaaQuality - 1;
}
else
{
sd.SampleDesc.Count = 1;
sd.SampleDesc.Quality = 0;
}
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
sd.BufferCount = 1;
sd.OutputWindow = mMainWnd;
sd.Windowed = true;
sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
sd.Flags = 0;
IDXGIDevice* dxgiDevice = 0;
HR(md3dDevice->QueryInterface(__uuidof(IDXGIDevice), (void**)&dxgiDevice));
IDXGIAdapter* dxgiAdapter = 0;
HR(dxgiDevice->GetParent(__uuidof(IDXGIAdapter), (void**)&dxgiAdapter));
IDXGIFactory* dxgiFactory = 0;
HR(dxgiAdapter->GetParent(__uuidof(IDXGIFactory), (void**)&dxgiFactory));
HR(dxgiFactory->CreateSwapChain(md3dDevice, &sd, &mSwapChain));
ReleaseCOM(dxgiDevice);
ReleaseCOM(dxgiAdapter);
ReleaseCOM(dxgiFactory);
ApplicationResize(mClientWidth, mClientHeight);
return true;
}
void D3DApp::CalculateFrameStats()
{
//Code computes the average frames per second also the average
//time it takes to render one frame. These stats
//are appended to the window caption bar.
static int frameCnt = 0;
static float timeElapsed = 0.0f;
frameCnt++;
float dt = mTimer.TotalTime() - timeElapsed;
if (dt >= 1.0f)
{
float fps = (float)frameCnt/dt;
float mspf = 1000.0f / fps;
std::wstringstream outs;
outs.precision(6);
outs << mMainWndCaption << L" "
<< L"FPS: " << fps << L" "
<< L"Frame Time: " << mspf << L" (ms)";
SetWindowText(mMainWnd, outs.str().c_str());
frameCnt = 0;
timeElapsed += dt;
}
}
void D3DApp::OnMouseDown(WPARAM btnState, int x, int y)
{
}
void D3DApp::OnMouseUp(WPARAM btnState, int x, int y)
{
}
void D3DApp::OnMouseMove(WPARAM btnState, int x, int y)
{
}

How to avoid excessive screen flickering with GDI

I'm sort of new to rendering graphics with GDI...
I made a paint program, and it works fine, it's just that it causes a lot of annoying screen flickering. I will admit my paint code is not really optimized (lack of time), but it shouldn't be super inefficient either, so I'm puzzled.
What I'm basically doing is creating a compatible DC on init, then create a compatible bitmap. Then I select it into the compatible DC, and paint to the compatible DC. Then I use BitBlit() to copy it to the window hDC...
Could anyone tell me the possible causes for this screen tearing?
EDIT: btw, screen flickering only occurs during the drawing of a path (before the path gets drawn to the hMemDC, it gets drawn to the hDC of the window)
Code samples:
(EDIT: If you need to see any more code that you think is relevant, comment and I'll edit)
Path::DrawTo(HDC)
bool Path::DrawTo(HDC hDC)
{
if(hDC == NULL || m_PointVector.size() <= 0) {
return false;
}
switch (m_Tool) {
case Tool_Pen:
{
Point2D p = m_PointVector.at(0);
if(m_PointVector.size() > 1) {
HPEN oldPen = (HPEN)SelectObject(hDC,m_hPen);
MoveToEx(hDC, p.x, p.y, nullptr);
for(UINT i = 1; i < m_PointVector.size(); ++i) {
p = m_PointVector.at(i);
LineTo(hDC,p.x,p.y);
}
SelectObject(hDC,oldPen);
break;
} //else
SetPixel(hDC,p.x-1,p.y,m_Col);
SetPixel(hDC,p.x,p.y,m_Col);
SetPixel(hDC,p.x+1,p.y,m_Col);
SetPixel(hDC,p.x,p.y-1,m_Col);
SetPixel(hDC,p.x,p.y+1,m_Col);
break;
}
case Tool_Line:
{
if(m_PointVector.size() > 1) {
Point2D p = m_PointVector.at(0);
HPEN oldPen = (HPEN)SelectObject(hDC,m_hPen);
MoveToEx(hDC, p.x, p.y, nullptr);
for(UINT i = 1; i < m_PointVector.size(); ++i) {
p = m_PointVector.at(i);
LineTo(hDC,p.x,p.y);
}
SelectObject(hDC,oldPen);
}
break;
}
case Tool_Ellipse:
{
if(m_PointVector.size() > 1) {
HPEN oldPen = (HPEN)SelectObject(hDC,m_hPen);
SelectObject(hDC,m_hBrush);
Point2D p1 = m_PointVector.at(0);
Point2D p2 = m_PointVector.at(1);
if(p1.x > p2.x) {
int iTemp = p1.x;
p1.x = p2.x;
p2.x = iTemp;
}
if(p1.y > p2.y) {
int iTemp = p1.y;
p1.y = p2.y;
p2.y = iTemp;
}
Ellipse(hDC,p1.x,p1.y,p2.x,p2.y);
SelectObject(hDC,oldPen);
}
break;
}
case Tool_Rectangle:
{
if(m_PointVector.size() > 1) {
HPEN oldPen = (HPEN)SelectObject(hDC,m_hPen);
SelectObject(hDC,m_hBrush);
Point2D p1 = m_PointVector.at(0);
Point2D p2 = m_PointVector.at(1);
if(p1.x > p2.x) {
int iTemp = p1.x;
p1.x = p2.x;
p2.x = iTemp;
}
if(p1.y > p2.y) {
int iTemp = p1.y;
p1.y = p2.y;
p2.y = iTemp;
}
Rectangle(hDC,p1.x,p1.y,p2.x,p2.y);
SelectObject(hDC,oldPen);
}
break;
}
case Tool_LineTrack:
{
HPEN oldPen = (HPEN)SelectObject(hDC,m_hPen);
SelectObject(hDC,m_hBrush);
int vSize = (int)m_PointVector.size();
Point2D p = m_PointVector.at(0);
if (vSize <= 1) {
Ellipse(hDC,p.x-10,p.y-10,p.x+10,p.y+10);
}
else {
//draw LineTrack
Point2D pTemp = m_PointVector.at(1);
MoveToEx(hDC,p.x,p.y,nullptr);
for (int i = 1; i < vSize; ++i) {
p = m_PointVector.at(i);
pTemp = m_PointVector.at(i-1);
LineTo(hDC,p.x,p.y);
Ellipse(hDC,pTemp.x-10,pTemp.y-10,pTemp.x+10,pTemp.y+10);
}
Ellipse(hDC,p.x-10,p.y-10,p.x+10,p.y+10);
}
SelectObject(hDC,oldPen);
break;
}
}
return true;
}
WndProc(HWND, UINT, WPARAM, LPARAM)
LRESULT MyApp::WndProc(HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
if(iMsg == WM_CREATE)
{
CREATESTRUCT *pCS = (CREATESTRUCT*)lParam;
SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG)pCS->lpCreateParams);
}
else
{
//retrieve the stored "this" pointer
MyApp* pApp = (MyApp*)GetWindowLongPtr(hWnd, GWLP_USERDATA);
switch (iMsg)
{
case WM_PAINT:
{
pApp->Paint();
return 0;
}
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
int wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_NEW:
{
////
return 0;
}
return 0;
case IDM_LOAD:
{
//////
return 0;
}
case IDM_SAVE:
{
//////
return 0;
}
case IDM_SAVEAS:
{
//////
return 0;
}
case IDM_COLOURMAIN:
{
COLORREF col;
if(MyWin32Funcs::OnColorPick(col)) {
pApp->m_pPath->SetColor1(col);
}
return 0;
}
case IDM_COLOURSECONDARY:
{
COLORREF col;
if(MyWin32Funcs::OnColorPick(col)) {
pApp->m_pPath->SetColor2(col);
}
return 0;
}
case IDM_PEN:
{
pApp->m_pPath->SetTool(Tool_Pen);
return 0;
}
case IDM_LINE:
{
pApp->m_pPath->SetTool(Tool_Line);
return 0;
}
case IDM_ELLIPSE:
{
pApp->m_pPath->SetTool(Tool_Ellipse);
return 0;
}
case IDM_RECTANGLE:
{
pApp->m_pPath->SetTool(Tool_Rectangle);
return 0;
}
case IDM_LINETRACK:
{
pApp->m_pPath->SetTool(Tool_LineTrack);
return 0;
}
default:
{
//////
return 0;
}
}
}
case WM_LBUTTONUP:
{
//////
Point2D p;
p.x = LOWORD(lParam);
p.y = HIWORD(lParam);
switch(pApp->m_pPath->GetTool()) {
case Tool_Pen:
{
pApp->m_bPaintToBitmap = true;
InvalidateRect(pApp->m_hWnd,NULL,true);
break;
}
case Tool_Ellipse:
{
pApp->m_bPaintToBitmap = true;
InvalidateRect(pApp->m_hWnd,NULL,true);
break;
}
case Tool_Rectangle:
{
pApp->m_bPaintToBitmap = true;
InvalidateRect(pApp->m_hWnd,NULL,true);
break;
}
case Tool_Line:
{
pApp->m_pPath->AddPoint(p,pApp->m_pBitmapPainter->GetBmpSize(),pApp->m_pBitmapPainter->GetBmpOffset());
InvalidateRect(pApp->m_hWnd,NULL,false);
break;
}
case Tool_LineTrack:
{
pApp->m_pPath->AddPoint(p,pApp->m_pBitmapPainter->GetBmpSize(),pApp->m_pBitmapPainter->GetBmpOffset());
InvalidateRect(pApp->m_hWnd,NULL,false);
break;
}
}
return 0;
}
case WM_RBUTTONUP:
{
//////
int x = LOWORD(lParam);
int y = HIWORD(lParam);
switch(pApp->m_pPath->GetTool()) {
case Tool_Line:
{
pApp->m_bPaintToBitmap = true;
InvalidateRect(pApp->m_hWnd,NULL,true);
break;
}
case Tool_LineTrack:
{
pApp->m_bPaintToBitmap = true;
InvalidateRect(pApp->m_hWnd,NULL,true);
break;
}
}
return 0;
}
case WM_LBUTTONDOWN:
{
Point2D p;
p.x = LOWORD(lParam);
p.y = HIWORD(lParam);
switch(pApp->m_pPath->GetTool()) {
case Tool_Pen:
pApp->m_pPath->AddPoint(p,pApp->m_pBitmapPainter->GetBmpSize(),pApp->m_pBitmapPainter->GetBmpOffset());
InvalidateRect(pApp->m_hWnd,NULL,false);
break;
}
}
case WM_MOUSEMOVE:
{
Point2D p;
p.x = LOWORD(lParam);
p.y = HIWORD(lParam);
if (wParam & MK_LBUTTON) {
switch(pApp->m_pPath->GetTool()) {
case Tool_Pen:
{
pApp->m_pPath->AddPoint(p,pApp->m_pBitmapPainter->GetBmpSize(),pApp->m_pBitmapPainter->GetBmpOffset());
InvalidateRect(pApp->m_hWnd,NULL,false);
break;
}
case Tool_Ellipse:
{
if( pApp->m_pPath->GetLen() >= 1) {
pApp->m_pPath->SetPointAt(1,p,pApp->m_pBitmapPainter->GetBmpSize(),pApp->m_pBitmapPainter->GetBmpOffset());
}
else {
pApp->m_pPath->AddPoint(p,pApp->m_pBitmapPainter->GetBmpSize(),pApp->m_pBitmapPainter->GetBmpOffset());
}
InvalidateRect(pApp->m_hWnd,NULL,false);
break;
}
case Tool_Rectangle:
{
if( pApp->m_pPath->GetLen() >= 1) {
pApp->m_pPath->SetPointAt(1,p,pApp->m_pBitmapPainter->GetBmpSize(),pApp->m_pBitmapPainter->GetBmpOffset());
}
else {
pApp->m_pPath->AddPoint(p,pApp->m_pBitmapPainter->GetBmpSize(),pApp->m_pBitmapPainter->GetBmpOffset());
}
InvalidateRect(pApp->m_hWnd,NULL,false);
break;
}
}
}
return 0;
}
case WM_CLOSE:
{
//////
return 0;
}
}
PostQuitMessage(0);
return 0;
}
}
}
return DefWindowProc (hWnd, iMsg, wParam, lParam) ;
}
MyApp::Paint()
void MyApp::Paint()
{
BeginPaint(m_hWnd,&m_PaintStruct);
if (m_bPaintToBitmap) {
Point2D p;
p.x = BMPXOFFSET;
p.y = BMPYOFFSET;
m_pPath->Offset(p);
m_pPath->DrawTo(m_pBitmapPainter->GetMemDC());
m_pPath->ClrPath();
m_pBitmapPainter->Paint(); //this is where BitBlt() occurs
m_bPaintToBitmap = false;
if(m_pBitmapPainter->IsAdjusted() == false) {
m_pBitmapPainter->SetbAdjusted(true);
}
}
else {
m_pBitmapPainter->Paint(); //this is where BitBlt() occurs
m_pPath->DrawTo(m_hDC);
}
EndPaint(m_hWnd,&m_PaintStruct);
}
Any help would much be appreciated.
I think what you're seeing is flicker, not tearing. To minimize flicker, your WM_PAINT should write to the window DC exactly once. Typically, this one operation is a BitBlt:
HDC hdc = BeginPaint(m_hwnd, &m_PaintStruct);
... paint to bitmap ...
BitBlt(hdc, ...); // blt from bitmap to screen
EndPaint(m_hwnd, &m_PaintStruct);
If you paint to the window DC in multiple steps, then you open the window for flicker.
Your description of the problem doesn't match the code. In your description, you say that you're blitting from the compatible DC to the window hDC. But in your code, your BitBlt is followed by a m_pPath->DrawTo(m_hDC). If a refresh occurs during the DrawTo, then the screen will refresh with a partially-drawn view, resulting in flicker.
If you are drawing the entire client area, override WM_ERASEBKGND, and simply return TRUE. That will reduce the flicker.
As others have pointed out; use the HDC given by WM_PAINT, as it may contain clipping regions, and other stuff that may optimize the screen update.
EDIT
If you are not drawing the entire client area, you can perform background painting in the areas you know where your WM_PAINT handler won't paint.
BeginPaint gives you HDC you are supposed to paint to. You are ignoring it.
What OS are you running on ? If its Vista or Windows 7, are you running in some sort of "compatibility mode" disabling desktop compositing ?
Supposedly one of the advantages of the Desktop Window Manager (DWM) introduced with Vista is (source):
In Windows XP, applications update their windows directly when the OS
requests them to. These requests could be executed asynchronously with
respect to the refresh rate of the monitor or to any updates that may
be currently running. The effect of these requests is that the user
sees windows tearing and re-drawing incorrectly or slowly. The DWM
style of window presentation eliminates the tearing artifacts,
providing a high quality desktop experience. The benefit to the end
user is that the system appears to be more responsive and the
experience is cleaner.
ie with the DWM, compositing to the desktop will be synchronized with drawing to avoid ever seeing partially drawn "torn" things.
If that's not it, are you sure you're not confusing "tearing" with background erase flicker or something ?

c++ using too much cpu

ok im creating a game but it uses too much cpu but it doesn't uses too much memory. the cpu does increase and decrease. i have too many timers in my game, i kill the timer when i don't use it any more so that should cause a problem but what i think that causes the problem is there is too many messages in my message qeue. i have new laptop it is 5 months old and it has a high cpu. it uses about 40% of my cpu. is there way to reduce it because when it gets to 40% the game slows down.
here is my code
// Xstrike.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "Xstrike.h"
#include <vector>
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
RECT *rect;
const UINT_PTR EVERYTHING_ID=0x1;
const UINT_PTR LBUTTONDOWN_ID=0x3;
const UINT_PTR TDENEMIE1_ID=0x4;
const UINT_PTR TAENEMIE1_ID=0x5;
const UINT_PTR PROTECTED_ID=0x6;
int conno=2;
int health=0;
int life=3;
int score=0;
int level=1;
int protect=0;
int cursor=0;
HCURSOR hCursor[3];
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
struct Enemies
{
int cEnemie1;
int dEnemie1;
int aEnemie1;
int sEnemie1;
int pEnemie1;
bool e1;
time_t now;
time_t tEnemie1;
vector <POINT> vS1Enemie1;
vector <POINT> vS2Enemie1;
};
vector <POINT> vRegularShots;
Enemies Enemie1;
VOID installising()
{
Enemie1.cEnemie1=0;
Enemie1.dEnemie1=1;
Enemie1.aEnemie1=0;
Enemie1.sEnemie1=1;
Enemie1.pEnemie1=550;
Enemie1.e1=true;
POINT pt;
pt.x=0;
pt.y=0;
vRegularShots.push_back(pt);
}
VOID Paint(HDC hdc, HWND hWnd)
{
int lifePos=200;
hdc=GetDC(hWnd);
HDC memDC=CreateCompatibleDC(hdc);
HBITMAP hMemMap=CreateCompatibleBitmap(hdc, 225, 350);
HBITMAP hOldMap=(HBITMAP)SelectObject(memDC, hMemMap);
Graphics draw(memDC);
// Drawing
Image bg(L"bg.jpg");
draw.DrawImage(&bg, 0, 0);
// if regular cursor
if(cursor==0)
{
// Regular shots
Image shot(L"RegularShots.png");
long s=vRegularShots.size();
// Draw shots
for(long index=0; index < (long)vRegularShots.size(); ++index)
{
draw.DrawImage(&shot, vRegularShots[index].x, vRegularShots[index].y);
}
// Update the shots
for(long index=0; index < (long)vRegularShots.size(); ++index)
{
vRegularShots[index].y--;
}
// Delete Shots
for(long index=0; index < (long)vRegularShots.size(); index++)
{
if(vRegularShots[index].y<-16)
{
vRegularShots.erase(vRegularShots.begin()+index);
}
}
// Create Enemies
if(Enemie1.dEnemie1==0)
{
if(Enemie1.cEnemie1<2)
{
if(Enemie1.aEnemie1==0)
{
SetTimer(hWnd, TAENEMIE1_ID, 550, NULL);
}
Enemie1.aEnemie1=1;
Enemie1.cEnemie1++;
}
else
{
KillTimer(hWnd, TDENEMIE1_ID);
}
Enemie1.dEnemie1=1;
}
// Draw enemies
for(long index=0; index < (long)Enemie1.vS1Enemie1.size(); ++index)
{
Image iEnemie1(L"Enemie1.png");
draw.DrawImage(&iEnemie1, Enemie1.vS1Enemie1[index].x, Enemie1.vS1Enemie1[index].y);
}
for(long index=0; index < (long)Enemie1.vS2Enemie1.size(); ++index)
{
Image iEnemie1(L"Enemie1.png");
draw.DrawImage(&iEnemie1, Enemie1.vS2Enemie1[index].x, Enemie1.vS2Enemie1[index].y);
}
// Update enemies
for(long index=0; index < (long)Enemie1.vS1Enemie1.size(); index++)
{
Enemie1.vS1Enemie1[index].x++;
Enemie1.vS1Enemie1[index].y++;
}
for(long index=0; index < (long)Enemie1.vS2Enemie1.size(); index++)
{
Enemie1.vS2Enemie1[index].x--;
Enemie1.vS2Enemie1[index].y++;
}
// Delete enemies
for(long index=0; index < (long)Enemie1.vS1Enemie1.size(); index++)
{
if(Enemie1.vS1Enemie1[index].x>225)
{
Enemie1.vS1Enemie1.erase(Enemie1.vS1Enemie1.begin()+index);
}
// Stop enemie1 timer
if(Enemie1.vS1Enemie1.size()==0&&Enemie1.vS2Enemie1.size()==0)
{
KillTimer(hWnd, TAENEMIE1_ID);
}
}
for(long index=0; index < (long)Enemie1.vS2Enemie1.size(); index++)
{
if(Enemie1.vS2Enemie1[index].x<-21)
{
Enemie1.vS2Enemie1.erase(Enemie1.vS2Enemie1.begin()+index);
}
// Stop enemie1 timer
if(Enemie1.vS1Enemie1.size()==0&&Enemie1.vS2Enemie1.size()==0)
{
KillTimer(hWnd, TAENEMIE1_ID);
}
}
// player hits
if(Enemie1.pEnemie1==550)
{
POINT pt;
GetCursorPos(&pt);
ScreenToClient(hWnd, &pt);
for(long index=0; index < (long)Enemie1.vS1Enemie1.size(); index++)
{
if(((pt.x+5)>=Enemie1.vS1Enemie1[index].x&&(pt.x+5)<=(Enemie1.vS1Enemie1[index].x+17)&&pt.y>=Enemie1.vS1Enemie1[index].y&&pt.y<=(Enemie1.vS1Enemie1[index].y+17))||((pt.x+15)>=Enemie1.vS1Enemie1[index].x&&pt.y>=Enemie1.vS1Enemie1[index].y&&pt.y<=(Enemie1.vS1Enemie1[index].y+17)&&(pt.x+14)<=(Enemie1.vS1Enemie1[index].x+17))||((pt.x+21)>=Enemie1.vS1Enemie1[index].x&&(pt.y+14)>=Enemie1.vS1Enemie1[index].y&&(pt.y+14)<=(Enemie1.vS1Enemie1[index].y+17)&&(pt.x+21)<=(Enemie1.vS1Enemie1[index].x+17))||((pt.x+23)>=Enemie1.vS1Enemie1[index].x&&(pt.y+22)>=Enemie1.vS1Enemie1[index].y&&(pt.y+22)<=(Enemie1.vS1Enemie1[index].y+17)&&(pt.x+23)<=(Enemie1.vS1Enemie1[index].x+17))||((pt.x+13)>=Enemie1.vS1Enemie1[index].x&&(pt.y+31)>=Enemie1.vS1Enemie1[index].y&&(pt.y+31)<=(Enemie1.vS1Enemie1[index].y+17)&&(pt.x+13)<=(Enemie1.vS1Enemie1[index].x+17))||(pt.x>=Enemie1.vS1Enemie1[index].x&&(pt.y+22)>=Enemie1.vS1Enemie1[index].y&&(pt.y+22)<=(Enemie1.vS1Enemie1[index].y+17)&&pt.x<=(Enemie1.vS1Enemie1[index].x+21))||((pt.x+3)>=Enemie1.vS1Enemie1[index].x&&(pt.y+14)>=Enemie1.vS1Enemie1[index].y&&(pt.y+14)<=(Enemie1.vS1Enemie1[index].y+21)&&(pt.x+3)<=(Enemie1.vS1Enemie1[index].x+21)))
{
health+=30;
Enemie1.pEnemie1=0;
InvalidateRect(hWnd, rect, false);
}
}
for(long index=0; index < (long)Enemie1.vS2Enemie1.size(); index++)
{
if(((pt.x+5)>=Enemie1.vS2Enemie1[index].x&&(pt.x+5)<=(Enemie1.vS2Enemie1[index].x+17)&&pt.y>=Enemie1.vS2Enemie1[index].y&&pt.y<=(Enemie1.vS2Enemie1[index].y+17))||((pt.x+15)>=Enemie1.vS2Enemie1[index].x&&pt.y>=Enemie1.vS2Enemie1[index].y&&pt.y<=(Enemie1.vS2Enemie1[index].y+17)&&(pt.x+14)<=(Enemie1.vS2Enemie1[index].x+17))||((pt.x+21)>=Enemie1.vS2Enemie1[index].x&&(pt.y+14)>=Enemie1.vS2Enemie1[index].y&&(pt.y+14)<=(Enemie1.vS2Enemie1[index].y+17)&&(pt.x+21)<=(Enemie1.vS2Enemie1[index].x+17))||((pt.x+23)>=Enemie1.vS2Enemie1[index].x&&(pt.y+22)>=Enemie1.vS2Enemie1[index].y&&(pt.y+22)<=(Enemie1.vS2Enemie1[index].y+17)&&(pt.x+23)<=(Enemie1.vS2Enemie1[index].x+17))||((pt.x+13)>=Enemie1.vS2Enemie1[index].x&&(pt.y+31)>=Enemie1.vS2Enemie1[index].y&&(pt.y+31)<=(Enemie1.vS2Enemie1[index].y+17)&&(pt.x+13)<=(Enemie1.vS2Enemie1[index].x+17))||(pt.x>=Enemie1.vS2Enemie1[index].x&&(pt.y+22)>=Enemie1.vS2Enemie1[index].y&&(pt.y+22)<=(Enemie1.vS2Enemie1[index].y+17)&&pt.x<=(Enemie1.vS2Enemie1[index].x+21))||((pt.x+3)>=Enemie1.vS2Enemie1[index].x&&(pt.y+14)>=Enemie1.vS2Enemie1[index].y&&(pt.y+14)<=(Enemie1.vS2Enemie1[index].y+21)&&(pt.x+3)<=(Enemie1.vS2Enemie1[index].x+21)))
{
health+=30;
Enemie1.pEnemie1=0;
InvalidateRect(hWnd, rect, false);
}
}
}
if(health>225)
{
cursor=1;
SetTimer(hWnd, PROTECTED_ID, 1000, NULL);
life--;
health=0;
InvalidateRect(hWnd, rect, false);
}
// Hits
for(long index=0; index < (long)vRegularShots.size(); index++)
{
for(long indexs=0; indexs < (long)Enemie1.vS1Enemie1.size(); indexs++)
{
if((vRegularShots[index].x>=Enemie1.vS1Enemie1[indexs].x && vRegularShots[index].y>=Enemie1.vS1Enemie1[indexs].y && vRegularShots[index].y<=(Enemie1.vS1Enemie1[indexs].y+17) && vRegularShots[index].x<=(Enemie1.vS1Enemie1[indexs].x+17))||(Enemie1.vS1Enemie1[indexs].x>=vRegularShots[index].x && Enemie1.vS1Enemie1[indexs].y>=vRegularShots[index].y && Enemie1.vS1Enemie1[indexs].y<=(vRegularShots[index].y+16) && Enemie1.vS1Enemie1[indexs].x<=(vRegularShots[index].x+5)))
{
Enemie1.vS1Enemie1.erase(Enemie1.vS1Enemie1.begin()+indexs);
vRegularShots[index].y=-17;
score+=100;
// Stop enemie1 timer
if(Enemie1.vS1Enemie1.size()==0&&Enemie1.vS2Enemie1.size()==0)
{
KillTimer(hWnd, TAENEMIE1_ID);
}
}
}
for(long indexs=0; indexs < (long)Enemie1.vS2Enemie1.size(); indexs++)
{
if((vRegularShots[index].x>=Enemie1.vS2Enemie1[indexs].x && vRegularShots[index].y>=Enemie1.vS2Enemie1[indexs].y && vRegularShots[index].y<=(Enemie1.vS2Enemie1[indexs].y+17) && vRegularShots[index].x<=(Enemie1.vS2Enemie1[indexs].x+17))||(Enemie1.vS2Enemie1[indexs].x>=vRegularShots[index].x && Enemie1.vS2Enemie1[indexs].y>=vRegularShots[index].y && Enemie1.vS2Enemie1[indexs].y<=(vRegularShots[index].y+16) && Enemie1.vS2Enemie1[indexs].x<=(vRegularShots[index].x+5)))
{
Enemie1.vS2Enemie1.erase(Enemie1.vS2Enemie1.begin()+indexs);
vRegularShots[index].y=-17;
score+=100;
// Stop enemie1 timer
if(Enemie1.vS1Enemie1.size()==0&&Enemie1.vS2Enemie1.size()==0)
{
KillTimer(hWnd, TAENEMIE1_ID);
}
}
}
}
}
// If protected
if(cursor==1)
{
if(protect!=4)
{
// Regular shots
Image shot(L"RegularShots.png");
// Draw shots
for(long index=0; index < (long)vRegularShots.size(); ++index)
{
draw.DrawImage(&shot, vRegularShots[index].x, vRegularShots[index].y);
}
// Update the shots
for(long index=0; index < (long)vRegularShots.size(); ++index)
{
vRegularShots[index].y--;
}
// Delete Shots
for(long index=0; index < (long)vRegularShots.size(); index++)
{
if(vRegularShots[index].y<-16)
{
vRegularShots.erase(vRegularShots.begin()+index);
}
}
// Delete Shots
for(long index=0; index < (long)vRegularShots.size(); index++)
{
if(vRegularShots[index].y<-16)
{
vRegularShots.erase(vRegularShots.begin()+index);
}
}
// Create Enemies
if(Enemie1.dEnemie1==0)
{
if(Enemie1.cEnemie1<2)
{
if(Enemie1.aEnemie1==0)
{
SetTimer(hWnd, TAENEMIE1_ID, 550, NULL);
}
Enemie1.aEnemie1=1;
Enemie1.cEnemie1++;
}
else
{
KillTimer(hWnd, TDENEMIE1_ID);
}
Enemie1.dEnemie1=1;
}
// Draw enemies
for(long index=0; index < (long)Enemie1.vS1Enemie1.size(); ++index)
{
Image iEnemie1(L"Enemie1.png");
draw.DrawImage(&iEnemie1, Enemie1.vS1Enemie1[index].x, Enemie1.vS1Enemie1[index].y);
}
for(long index=0; index < (long)Enemie1.vS2Enemie1.size(); ++index)
{
Image iEnemie1(L"Enemie1.png");
draw.DrawImage(&iEnemie1, Enemie1.vS2Enemie1[index].x, Enemie1.vS2Enemie1[index].y);
}
// Update enemies
for(long index=0; index < (long)Enemie1.vS1Enemie1.size(); index++)
{
Enemie1.vS1Enemie1[index].x++;
Enemie1.vS1Enemie1[index].y++;
}
for(long index=0; index < (long)Enemie1.vS2Enemie1.size(); index++)
{
Enemie1.vS2Enemie1[index].x--;
Enemie1.vS2Enemie1[index].y++;
}
// Delete enemies
for(long index=0; index < (long)Enemie1.vS1Enemie1.size(); index++)
{
if(Enemie1.vS1Enemie1[index].x>225)
{
Enemie1.vS1Enemie1.erase(Enemie1.vS1Enemie1.begin()+index);
}
// Stop enemie1 timer
if(Enemie1.vS1Enemie1.size()==0&&Enemie1.vS2Enemie1.size()==0)
{
KillTimer(hWnd, TAENEMIE1_ID);
}
}
}
else
{
KillTimer(hWnd, PROTECTED_ID);
cursor=0;
protect=0;
}
}
// Draw Lifes
for(long index=0; index != life; index++)
{
Image lifes(L"lifes.png");
draw.DrawImage(&lifes, lifePos, 275);
lifePos-=30;
}
SolidBrush textColor(Color(255, 255, 54, 0));
LinearGradientBrush linGrBrush(
Point(1000, 20),
Point(health, 20),
Color(255, 255, 0, 0), // opaque black
Color(0, 0, 0, 0)); // opaque red
SolidBrush databrush(Color(0, 225, 0));
draw.FillRectangle(&databrush, 0, 0, 225, 30);
draw.FillRectangle(&linGrBrush, 0, 0, 225, 30);
FontFamily courieNew(L"Courier New");
Font font(&courieNew, 12, 0, UnitPixel);
PointF pLevel(5.0f, 6.0f);
PointF pScore(100.0f, 6.0f);
WCHAR wLevel[11];
WCHAR wScore[60];
_swprintf(wLevel, L"Level : %d", level);
_swprintf(wScore, L"Score : %d", score);
draw.DrawString(wLevel, -1, &font, pLevel, &textColor);
draw.DrawString(wScore, -1, &font, pScore, &textColor);
BitBlt(hdc, 0, 0, 225, 350, memDC, 0, 0, SRCCOPY);
ReleaseDC(hWnd, hdc);
SelectObject(memDC, hOldMap);
DeleteObject(hMemMap);
DeleteDC(memDC);
}
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
// Cursors
hCursor[0]=LoadCursor(hInstance, MAKEINTRESOURCE(IDC_CURSOR));
hCursor[1]=LoadCursor(hInstance, MAKEINTRESOURCE(IDC_PROTECTED));
// Initialize GDI+.
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_XSTRIKE, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_XSTRIKE));
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
GdiplusShutdown(gdiplusToken);
return (int) msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage are only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_XSTRIKE));
wcex.hCursor = LoadCursor(hInstance, MAKEINTRESOURCE(IDC_CURSOR));
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_XSTRIKE);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&wcex);
}
//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX),
CW_USEDEFAULT, 0, 225, 350, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
POINT pt;
switch (message)
{
case WM_CREATE:
installising();
SetTimer(hWnd, EVERYTHING_ID, 1, NULL);
break;
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
Paint(hdc, hWnd);
EndPaint(hWnd, &ps);
break;
case WM_LBUTTONDOWN:
GetCursorPos(&pt);
ScreenToClient(hWnd, &pt);
if(conno==1)
{
conno++;
}
else
{
pt.x+=18;
conno--;
}
vRegularShots.push_back(pt);
SetTimer(hWnd, LBUTTONDOWN_ID, 350, NULL);
InvalidateRect(hWnd, rect, false);
break;
case WM_LBUTTONUP:
KillTimer(hWnd, LBUTTONDOWN_ID);
break;
case WM_TIMER:
switch(wParam)
{
case EVERYTHING_ID:
if(Enemie1.e1==true)
{
Enemie1.now=time(NULL);
Enemie1.tEnemie1=Enemie1.now+1;
Enemie1.e1=false;
}
Enemie1.now=time(NULL);
if(Enemie1.now==Enemie1.tEnemie1)
{
SetTimer(hWnd, TDENEMIE1_ID, 550, NULL);
}
InvalidateRect(hWnd, rect, false);
break;
case LBUTTONDOWN_ID:
GetCursorPos(&pt);
ScreenToClient(hWnd, &pt);
if(conno==1)
{
if(cursor==0)
{
conno++;
}
else if(cursor==1)
{
pt.x+=2;
conno++;
}
}
else
{
if(cursor==0)
{
pt.x+=18;
}
else if(cursor==1)
{
pt.x+=15;
}
conno--;
}
vRegularShots.push_back(pt);
break;
case TDENEMIE1_ID:
pt.y=5;
pt.x=-21;
Enemie1.vS1Enemie1.push_back(pt);
pt.y=5;
pt.x=219;
Enemie1.vS2Enemie1.push_back(pt);
Enemie1.dEnemie1=0;
InvalidateRect(hWnd, rect, false);
break;
case TAENEMIE1_ID:
if(Enemie1.pEnemie1!=550)
{
Enemie1.pEnemie1+=550;
}
InvalidateRect(hWnd, rect, false);
break;
case PROTECTED_ID:
protect++;
break;
}
break;
case WM_SETCURSOR:
SetCursor(hCursor[cursor]);
return 0;
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
any idea?
My guess would be that you are constantly re-allocating and re-drawing everything instead simply moving the images around. I can see the application frequently having to re-allocate and re-draw from scratch having a bottleneck on the system.
You can optimize a little the paint function.
For example the creation of the four bitmaps could be done in an initialization phase since it is always the same files you are loading.
Also, in all the for loops, you can make one call to the size function of your vector, store it in a temporary variable and use that variable in the for loop for the condition ( as long as the size of the vectors doesn't change in the for loops).
It's very hard to say without knowing more about usage patterns of the thing. A good profiler will do wonders - I've had good luck on Windows with AQTime, although it's not free which may not suit you.
A few observations which may or may not be relevant:
You are drawing using GDI+. It is not fast and may well not scale to what you need. If I wanted rendering of complex scenes, I'd find something else.
I am suspicious about you calling InvalidateRect in your Paint function. Seems odd to me (doesn't painting validate the image?). Also, you are still repainting everything regardless, so what's the point?
You are using erase() on a vector to erase multiple (possibly many) random entries. This has quadratic performance and should be avoided for large vectors.
In one spot there's a pair of nested loops (loop over all 'shots' followed by loop over all 'enemies'); those look like they'll scale badly to large numbers of both.
But you really need to find a profiler; I'm just guessing here.
Please try with the profiler that comes with VS 2010 details of which are here
That may be your best bet on this.