심플한 Tetris를 만들어 보자 - 화면에 이미지를 그려봐요.

2010. 8. 16. 15:15 장난감

Wave용 어플을 만들어 보기로 했는데, 한번만에 안나오더라 몇번의 삽질 후 보이기 시작한 화면 조금씩 하는거다. 그냥 취미일 뿐.


우선 badaSDK를 설치 후 (본인은 1.0.0b3로 했다)

아래 소스에서 붉은색 부분을 추가하여 실행하여 보자.

  O
OOO

이런 모양이 하면 하단으로 내려온다.



Tetris.h
#ifndef __TETRIS_H__
#define __TETRIS_H__

#include <FApp.h>
#include <FBase.h>
#include <FGraphics.h>
#include <FSystem.h>
#include <FMedia.h>
#include <FUi.h>

/**
 * [Tetris] application must inherit from Application class
 * which provides basic features necessary to define an application.
 */
class Tetris :
public Osp::App::Application,
public Osp::System::IScreenEventListener,
public Osp::Base::Runtime::ITimerEventListener
{
public:
// [Tetris] application must have a factory method that creates an instance of itself.
static Osp::App::Application* CreateInstance(void);

public:
Tetris();
~Tetris();

public:


// Called when the application is initializing.
bool OnAppInitializing(Osp::App::AppRegistry& appRegistry);

// Called when the application is terminating.
bool OnAppTerminating(Osp::App::AppRegistry& appRegistry, bool forcedTermination = false);

// Called when the application's frame moves to the top of the screen.
void OnForeground(void);

// Called when this application's frame is moved from top of the screen to the background.
void OnBackground(void);

// Called when the system memory is not sufficient to run the application any further.
void OnLowMemory(void);

// Called when the battery level changes.
void OnBatteryLevelChanged(Osp::System::BatteryLevel batteryLevel);

// Called when the screen turns on.
void OnScreenOn (void);

// Called when the screen turns off.
void OnScreenOff (void);

void OnTimerExpired(Osp::Base::Runtime::Timer& timer);

void Draw();

private:
Osp::Base::Runtime::Timer* _pTimer;
Osp::Graphics::Bitmap* _pBitmap;
};

const int blockXY[] = {
0,  0,
-1,  0,
0, -1,
1,  0
};

#endif //__TETRIS_H__


Tetris.cpp

/**
 * Name        : Tetris
 * Version     : 
 * Vendor      : 
 * Description : 
 */

#include "Tetris.h"

using namespace Osp::App;
using namespace Osp::Base;
using namespace Osp::Base::Runtime;
using namespace Osp::System;
using namespace Osp::Media;
using namespace Osp::Graphics;
using namespace Osp::Ui;

Tetris::Tetris()
{
_pBitmap = null;
}

Tetris::~Tetris()
{
}

Application*
Tetris::CreateInstance(void)
{
// Create the instance through the constructor.
return new Tetris();
}

bool
Tetris::OnAppInitializing(AppRegistry& appRegistry)
{
// TODO:
// Initialize UI resources and application specific data.
// The application's permanent data and context can be obtained from the appRegistry.
//
// If this method is successful, return true; otherwise, return false.
// If this method returns false, the application will be terminated.

// Uncomment the following statement to listen to the screen on/off events.
//PowerManager::SetScreenEventListener(*this);

if(_pBitmap == null)
{
Image* _pImage = new Image;
_pImage->Construct();
_pBitmap = _pImage->DecodeN(L"/Res/block.png", BITMAP_PIXEL_FORMAT_ARGB8888);

delete _pImage;
}

_pTimer = new Timer;
_pTimer->Construct(*this);

return true;
}

bool
Tetris::OnAppTerminating(AppRegistry& appRegistry, bool forcedTermination)
{
// TODO:
// Deallocate resources allocated by this application for termination.
// The application's permanent data and context can be saved via appRegistry.

if(_pBitmap)
{
delete _pBitmap;
}

return true;
}

void
Tetris::OnForeground(void)
{
// TODO:
// Start or resume drawing when the application is moved to the foreground.

if(_pTimer)
{
_pTimer->Start(1000);
}
}

void
Tetris::OnBackground(void)
{
// TODO:
// Stop drawing when the application is moved to the background.

if(_pTimer)
{
_pTimer->Cancel();
}
}

void
Tetris::OnLowMemory(void)
{
// TODO:
// Free unused resources or close the application.
}

void
Tetris::OnBatteryLevelChanged(BatteryLevel batteryLevel)
{
// TODO:
// Handle any changes in battery level here.
// Stop using multimedia features(camera, mp3 etc.) if the battery level is CRITICAL.
}

void
Tetris::OnScreenOn (void)
{
// TODO:
// Get the released resources or resume the operations that were paused or stopped in OnScreenOff().
}

void
Tetris::OnScreenOff (void)
{
// TODO:
//  Unless there is a strong reason to do otherwise, release resources (such as 3D, media, and sensors) to allow the device to enter the sleep mode to save the battery.
// Invoking a lengthy asynchronous method within this listener method can be risky, because it is not guaranteed to invoke a callback before the device enters the sleep mode.
// Similarly, do not perform lengthy operations in this listener method. Any operation must be a quick one.
}

void
Tetris::OnTimerExpired(Timer& timer)
{
if(!_pTimer)
return;

_pTimer->Start(1000);

Draw();
}

void
Tetris::Draw()
{
static int down = 50;
AppLog("Draw...");

Canvas* pCanvas = GetAppFrame()->GetCanvasN();
pCanvas->Construct();
pCanvas->Clear();

int width = _pBitmap->GetWidth();
int height = _pBitmap->GetHeight();
int len = sizeof(blockXY) / sizeof(blockXY[0]);

for(int i = 0; i < len; i+=2)
pCanvas->DrawBitmap(*(new Point(100+(width*blockXY[i]), down+(height*blockXY[i+1]))), *_pBitmap);

down += height;
if(down >= 700) {
down = 50;
}

pCanvas->Show();

delete pCanvas;
}



즐~!


' 장난감' 카테고리의 다른 글

OpenMP를 찾다.  (0) 2010.08.24
OpenGL 사용하기  (0) 2010.08.17
Jar정보 검색  (0) 2010.08.12
redmine 을 설치하다.  (0) 2010.07.08
badaSDK로 만든 binary를 bada시뮬레이터에 띄워보자는 취지  (0) 2010.06.10