码迷,mamicode.com
首页 > 其他好文 > 详细

Hello world

时间:2015-07-11 10:34:13      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:

#include <stdio.h>

#include <list>

#include <iostream>

#include <vector>

#include <SDL.h>

#include <SDL_image.h>

const int SCREEN_WIDTH = 640;

const int SCREEN_HEIGHT = 480;

?

SDL_Window* gWindow = NULL;

SDL_Renderer* gRenderer = NULL;

namespace Xgui

{

?

?

?

class Plane

{

private:

int m_x;

int m_y;

?

//must >0

int m_width;

//must >0

int m_height;

?

uint8_t r;

?

long unsigned m_id;

?

//just add 1 ,for m_id

static int total_count;

?

?

public:

Plane() : m_x ( 0 ), m_y ( 0 ), m_height ( 1 ), m_width ( 1 ), r ( 0xff )

{

Plane::total_count++;

m_id = Plane::total_count;

}

~Plane()

{

?

}

?

bool inField ( int x_pos, int y_pos )

{

if ( ( x_pos < m_x + m_width ) && ( x_pos >= m_x ) )

{

if ( ( y_pos < m_y + m_height ) && ( y_pos >= m_y ) )

{

return true;

}

}

?

return false;

}

?

?

void render()

{

SDL_SetRenderDrawBlendMode ( gRenderer, SDL_BLENDMODE_BLEND );

SDL_Rect fillRect = {m_x, m_y, m_width, m_height };

SDL_SetRenderDrawColor ( gRenderer, r, 0x00, 0x00, 0xee );

?

SDL_RenderFillRect ( gRenderer, &fillRect );

}

?

int update ( const SDL_Event& e )

{

static bool down = false;

static int x_plane;

static int y_plane;

static int x_mouse;

static int y_mouse;

?

std::cout << "i am:" << this->m_id << std::endl;

?

if ( e.type == SDL_MOUSEBUTTONDOWN )

{

down = true;

x_plane = this->m_x;

y_plane = this->m_y;

SDL_GetMouseState ( &x_mouse, &y_mouse );

?

std::cout << "down" << std::endl;

return 1;

?

}

?

else if ( e.type == SDL_MOUSEMOTION )

{

if ( down )

{

int xpos, ypos;

SDL_GetMouseState ( &xpos, &ypos );

m_x = x_plane - x_mouse + xpos;

m_y = y_plane - y_mouse + ypos;

}

?

}

else if ( e.type == SDL_MOUSEBUTTONUP )

{

down = false;

}

else if ( e.type == SDL_MOUSEWHEEL )

{

std::cout << "wheel:" << e.wheel.y;

std::cout << "motion or up or wheel" << std::endl;

}

?

?

?

else

{

std::cout << "other" << std::endl;

}

?

return 0;

}

?

void set ( int x, int y, int w, int h, uint8_t r )

{

m_x = x;

m_y = y;

m_width = w;

m_height = h;

this->r = r;

}

};

int Plane::total_count = -1;

?

?

?

?

?

class thePlaneManger

{

public:

void render()

{

if ( planes.empty() == true )

return ;

?

?

for ( int i = 0; i < planes.size(); i++ )

{

planes[i]->render();

}

}

void update ( const SDL_Event& e )

{

?

if ( planes.empty() == true )

return ;

?

?

//==========================================================

static int i = 0;

?

if ( i == 0 )

{

std::cout << "=======================-" << std::endl;

i = 1;

}

else

{

i = 0;

std::cout << "=======================+" << std::endl;

}

?

//===========================================================

?

?

std::cout << "size is:" << this->get_size() << std::endl;

?

if (

e.type == SDL_MOUSEMOTION ||

e.type == SDL_MOUSEBUTTONUP ||

e.type == SDL_MOUSEBUTTONDOWN ||

e.type == SDL_MOUSEWHEEL

)

{

int x, y;

SDL_GetMouseState ( &x, &y );

?

//依次遍历每个平面,判断鼠标是否处于该平面

for ( int i = planes.size() - 1; i >= 0; i-- )

{

bool flag = planes[i]->inField ( x, y );

?

if ( flag == true )

{

//鼠标落在了第i个平面上

int m = planes[i]->update ( e );

?

if ( m == 1 )

{

this->push_back ( planes[i] );

this->remove ( i );

?

}

?

return ;

}

}

?

//鼠标没有落在任何平面上,直接返回

?

return ;

}

else

{

//因为最顶层的平面定义为激活平面,所以由他来接受当前事件

planes[planes.size() - 1]->update ( e );

return ;

}

?

}

static int size;

int get_size()

{

return planes.size();

}

bool empty()

{

return planes.empty();

}

?

bool remove ( long unsigned order )

{

?

if ( planes.empty() == true )

{

return false;

}

?

if ( order >= 0 && order < planes.size() )

{

planes.erase ( planes.begin() + order );

return true;

}

?

return false;

?

}

void push_back ( Plane* plane )

{

planes.push_back ( plane );

}

void push_front ( Plane* plane )

{

planes.insert ( planes.begin(), plane );

}

Plane* get ( long unsigned int order )

{

if ( planes.empty() == true )

{

return NULL;

}

else

{

if ( order >= 0 && order < planes.size() )

?

return planes[order];

else

return NULL;

}

}

?

?

?

static thePlaneManger* Instance()

{

?

?

if ( thePlaneManger::manager == NULL )

{

thePlaneManger::manager = new thePlaneManger();

}

else

{

?

}

?

return thePlaneManger::manager;

}

private:

?

static thePlaneManger* manager;

?

static std::vector<Plane*> planes;

};

thePlaneManger* thePlaneManger::manager = NULL;

std::vector<Plane*> thePlaneManger::planes;

?

?

?

}

?

?

?

void get_Event ( const SDL_Event& e )

{

Xgui::thePlaneManger::Instance()->update ( e );

}

?

bool init()

{

//Initialization flag

bool success = true;

?

//Initialize SDL

if ( SDL_Init ( SDL_INIT_VIDEO ) < 0 )

{

printf ( "SDL could not initialize! SDL Error: %s\n", SDL_GetError() );

success = false;

}

else

{

//Set texture filtering to linear

if ( !SDL_SetHint ( SDL_HINT_RENDER_SCALE_QUALITY, "1" ) )

{

printf ( "Warning: Linear texture filtering not enabled!" );

}

?

//Create window

gWindow = SDL_CreateWindow ( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );

?

if ( gWindow == NULL )

{

printf ( "Window could not be created! SDL Error: %s\n", SDL_GetError() );

success = false;

}

else

{

//Create renderer for window

gRenderer = SDL_CreateRenderer ( gWindow, -1, SDL_RENDERER_ACCELERATED );

?

if ( gRenderer == NULL )

{

printf ( "Renderer could not be created! SDL Error: %s\n", SDL_GetError() );

success = false;

}

else

{

//Initialize renderer color

SDL_SetRenderDrawColor ( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );

?

//Initialize PNG loading

int imgFlags = IMG_INIT_PNG;

?

if ( ! ( IMG_Init ( imgFlags ) & imgFlags ) )

{

printf ( "SDL_image could not initialize! SDL_mage Error: %s\n", IMG_GetError() );

success = false;

}

}

}

}

?

return success;

}

void close()

{

?

?

//Destroy window

SDL_DestroyRenderer ( gRenderer );

SDL_DestroyWindow ( gWindow );

gWindow = NULL;

gRenderer = NULL;

?

//Quit SDL subsystems

IMG_Quit();

SDL_Quit();

}

int main ( int argc, char **argv )

{

?

using namespace Xgui;

?

Plane* tmp1 = new Plane();

Plane* tmp2 = new Plane();

tmp1->set ( 0, 0, 200, 200 , 0x00 );

tmp2->set ( 100, 100, 200, 200 , 0xff );

?

thePlaneManger::Instance()->push_back ( tmp1 );

thePlaneManger::Instance()->push_back ( tmp2 );

?

if ( init() == true )

{

?

bool quit = false;

?

//Event handler

SDL_Event e;

?

//While application is running

while ( !quit )

{

?

//Handle events on queue

/*

*/

?

?

if ( 1 )

{

while ( SDL_PollEvent ( &e ) != 0 )

{

//User requests quit

if ( e.type == SDL_QUIT )

{

quit = true;

}

?

get_Event ( e );

}

}

else

{

SDL_WaitEvent ( &e );

?

if ( e.type == SDL_QUIT )

quit = true;

?

get_Event ( e );

}

?

SDL_Delay ( 1 );

//Clear screen

SDL_SetRenderDrawColor ( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );

SDL_RenderClear ( gRenderer );

?

thePlaneManger::Instance()->render();

//Update screen

SDL_RenderPresent ( gRenderer );

}

?

}

?

close();

?

printf ( "hello world\n" );

?

return 0;

}

Hello world

标签:

原文地址:http://www.cnblogs.com/Dream-Chaser/p/4638063.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!