Beispiel #1
0
void COpenGL::updateScreen()
{
	glEnable(GL_TEXTURE_2D);
	// Set up an array of values to use as the sprite vertices.
	GLfloat vertices[] =
	{
		0, 0,
		1, 0,
		1, 1,
		0, 1,
	};

	// Set up an array of values for the texture coordinates.
	GLfloat texcoords[] =
	{
		0, 0,
		1, 0,
		1, 1,
		0, 1,
	};

	//Render the vertices by pointing to the arrays.
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

	glVertexPointer(2, GL_FLOAT, 0, vertices);
	glTexCoordPointer(2, GL_FLOAT, 0, texcoords);

	glEnable(GL_BLEND);

	if(m_VidConfig.m_ScaleXFilter > 1)
	{
		if(getPerSurfaceAlpha(FXSurface))
			SDL_BlitSurface(FXSurface, NULL, BlitSurface, NULL);
	}

	loadSurface(m_texture, BlitSurface);
	renderTexture(m_texture);


	if(m_VidConfig.m_ScaleXFilter == 1)
	{
		if(FXSurface && getPerSurfaceAlpha(FXSurface))
		{
			reloadFX(FXSurface);
			renderTexture(m_texFX, true);
		}
	}

	glDisableClientState(GL_VERTEX_ARRAY);
	glDisableClientState(GL_TEXTURE_COORD_ARRAY);
	glDisable(GL_BLEND);
	glDisable(GL_TEXTURE_2D);

	g_pInput->renderOverlay();

	SDL_GL_SwapBuffers();
}
void LLDrawPoolGlow::render(S32 pass)
{
	LLFastTimer t(LLFastTimer::FTM_RENDER_GLOW);
	LLGLEnable blend(GL_BLEND);
	LLGLDisable test(GL_ALPHA_TEST);
	gGL.setSceneBlendType(LLRender::BT_ADD);
	
	U32 shader_level = LLViewerShaderMgr::instance()->getVertexShaderLevel(LLViewerShaderMgr::SHADER_OBJECT);

	if (shader_level > 0 && fullbright_shader)
	{
		fullbright_shader->bind();
	}
	else
	{
		gPipeline.enableLightsFullbright(LLColor4(1,1,1,1));
	}

	LLGLDepthTest depth(GL_TRUE, GL_FALSE);
	gGL.setColorMask(false, true);
	renderTexture(LLRenderPass::PASS_GLOW, getVertexDataMask());
	
	gGL.setColorMask(true, false);
	gGL.setSceneBlendType(LLRender::BT_ALPHA);
	
	if (shader_level > 0 && fullbright_shader)
	{
		fullbright_shader->unbind();
	}
}
void LLDrawPoolSimple::render(S32 pass)
{
	LLGLDisable blend(GL_BLEND);
	
	{ //render simple
		LLFastTimer t(FTM_RENDER_SIMPLE);
		gPipeline.enableLightsDynamic();

		if (mVertexShaderLevel > 0)
		{
			U32 mask = getVertexDataMask() | LLVertexBuffer::MAP_TEXTURE_INDEX;

			pushBatches(LLRenderPass::PASS_SIMPLE, mask, TRUE, TRUE);

			if (LLPipeline::sRenderDeferred)
			{ //if deferred rendering is enabled, bump faces aren't registered as simple
				//render bump faces here as simple so bump faces will appear under water
				pushBatches(LLRenderPass::PASS_BUMP, mask, TRUE, TRUE);
			}
		}
		else
		{
			LLGLDisable alpha_test(GL_ALPHA_TEST);
			renderTexture(LLRenderPass::PASS_SIMPLE, getVertexDataMask());
		}
		
	}
}
Beispiel #4
0
MainWindow::MainWindow()
{
    setupUi(this); 

    connect(action_Load_Model, SIGNAL(triggered()), this, SLOT(loadModel()));
    connect(action_Snap_Shot, SIGNAL(triggered()), this, SLOT(snapShot()));
    connect(action_Fix_Camera, SIGNAL(triggered()), this, SLOT(fixCamera()));
    connect(action_Reset_Screen, SIGNAL(triggered()), this, SLOT(resetScreen()));
    connect(action_Update_Geometry, SIGNAL(triggered()), this, SLOT(updateGeometry()));
    connect(action_Export_OBJ, SIGNAL(triggered()), this, SLOT(exportOBJ()));
    connect(action_Render, SIGNAL(triggered()), this, SLOT(renderTexture()));
    connect(action_Vector_Field, SIGNAL(triggered()), this, SLOT(setVectorField()));
    connect(action_Load_2D_3D_points, SIGNAL(triggered()), this, SLOT(loadPoints()));
    connect(action_Tool_Box, SIGNAL(triggered()), this, SLOT(showToolBox()));
    connect(action_Delete_Last_Line_Of_Source, SIGNAL(triggered()), this, SLOT(deleteLastLine_Source()));
    connect(action_Delete_Last_Line_Of_Target, SIGNAL(triggered()), this, SLOT(deleteLastLine_Target()));

    disp_modules.reset(new DispModuleHandler(centralwidget));
    this->setCentralWidget(centralwidget);

    parameter_dock.reset(new ParameterDock);
    parameter_dock->setFixedWidth(250);
    this->addDockWidget(Qt::LeftDockWidgetArea, parameter_dock.get());
    parameter_dock->setDispModules(disp_modules);
    parameter_dock->hide();

    this->show();

	//pointsSelect = false;
	//isCompute = false;
}
Beispiel #5
0
void TextLine::render()
{
  if (mActive)
    {
      renderTexture(mTexture, mTextMaker->mRenderer, mPosX, mPosY, mWidth, mHeight);
    }
}
Beispiel #6
0
void Button::render(bool resetPos)
{
  //std::cout << "INFO: Button::render: Rendering the Button" << std::endl;
  float posx = getPosX();
  float posy = getPosY();
  float height = getHeight();
  float width = getWidth();
  if ( resetPos )
    setPositions();
  posx = getPosX();
  posy = getPosY();
  height = getHeight();
  width = getWidth();
  renderTexture( mTexture, mRenderer, posx, posy, width, height );
  //render the title message
  if (mTitle != nullptr)
    {
      float title_scale = 0.7;
      mTitle->setActive(true);
      mTitle->setHeight(height * title_scale);
      mTitle->setWidth(width * title_scale);
      float tborder = (1.-0.8)/2.;
      mTitle->setPosX( posx + tborder * width);
      mTitle->setPosY( posy + tborder * height);
      mTitle->render();
    }
}
void draw_game_running(SDL_Renderer* ren)
{
    int bW, bH;
    SDL_QueryTexture(background, NULL, NULL, &bW, &bH);
    renderTexture(background, ren, 0, 0);

    //draw the fixed peices
    int i, j, x, y;
    for(i=0; i<10; i++) {
        for(j=0; j<16; j++) {
            x =  GRID_START_X + BLOCK_SIZE * i;
            y =  GRID_START_Y + BLOCK_SIZE * j;
            int color = grid[i][j];
            if(color>0)
                renderTextureClip(blocks, ren, x, y, &blockClips[color-1]);
        }
    }

    DROP *d = (DROP*)dropData+useClip+1;
    int r = rotation%(d->rotations);
    PIECE *p = &(d->peice[r]);
    x = drop_x;
    y = drop_y;
    drawPiece(ren, p, x, y, useClip);
    
    //Draw the next piece to the right
    DROP *dN = (DROP*)dropData+nextPiece+1;
    PIECE *pN = &(dN->peice[0]); //rotation 0
    x = drop_x;
    y = drop_y;
    drawPiece(ren, pN, NEXT_PIECE_X, NEXT_PIECE_Y, nextPiece);
}
Beispiel #8
0
/**
* Draw an SDL_Texture to an SDL_Renderer at position x, y, preserving
* the texture's width and height
* @param tex The source texture we want to draw
* @param ren The renderer we want to draw to
* @param x The x coordinate to draw to
* @param y The y coordinate to draw to
*/
void CKT::GFX::renderTexture(SDL_Texture *tex, SDL_Renderer *ren, int x, int y) 
{
	int w = 0;
	int h = 0;
	//Query the texture to get its width and height to use
	SDL_QueryTexture(tex, NULL, NULL, &w, &h);
	renderTexture(tex, ren, x, y, w, h);
}
Beispiel #9
0
		/**
		 *\~english
		 *\brief		Renders the given 2D texture to the currently draw-bound frame buffer.
		 *\param[in]	size		The render viewport size.
		 *\param[in]	texture		The texture.
		 *\~french
		 *\brief		Rend la texture 2D donnée dans le tampon d'image actuellement activé en dessin.
		 *\param[in]	size		La taille du viewport de rendu.
		 *\param[in]	texture		La texture.
		 */
		inline void renderTexture( castor::Size const & size
			, TextureLayout const & texture )
		{
			static castor::Position const position;
			renderTexture( position
				, size
				, texture );
		}
Beispiel #10
0
void TextureWarpShader::renderTexture(GLuint target, GLuint id, const Eigen::Matrix3fr &homography, const Eigen::Vector2i &imageSize,
	const Eigen::Vector2f &screenOrigin)
{
	std::vector<Eigen::Vector4f> vertices;
	std::vector<Eigen::Vector2f> textureCoords;
	TextureShader::CreateVertices(screenOrigin, imageSize, vertices, textureCoords);
    renderTexture(GL_TRIANGLE_STRIP, target, id, homography, imageSize, vertices.data(), textureCoords.data(), 4);
}
Beispiel #11
0
void Game::render() {
    if (gameRunning) {
        SDL_RenderClear(mainRenderer);
    
        if (mainTexture) {
            renderTexture(mainTexture, mainRenderer, m_x, m_y);
        }
    
        SDL_RenderPresent(mainRenderer);
    }
}
Beispiel #12
0
void Small::Draw()
{
	if (TeamID == NONE)
	{
		renderTexture(PlayerTex, Renderer, (XMVectorGetX(Location) - 4.0f), (XMVectorGetY(Location) - 4.0f));
	}
	else if (TeamID == RED)
	{
		renderTexture(RedTex, Renderer, (XMVectorGetX(Location) - 4.0f), (XMVectorGetY(Location) - 4.0f));
	}
	else //(TeamID == BLUE)
	{
		renderTexture(BlueTex, Renderer, (XMVectorGetX(Location) - 4.0f), (XMVectorGetY(Location) - 4.0f));
	}

	//renderTexture(PointingTex, Renderer, (XMVectorGetX(PointingOneLocation) - 8.0f), (XMVectorGetY(PointingOneLocation) - 8.0f));
	if (TeamID != NONE)
	{
		renderTexture(PointingTex, Renderer, (XMVectorGetX(PointingTwoLocation) - 2.0f), (XMVectorGetY(PointingTwoLocation) - 2.0f));
	}
}
Beispiel #13
0
		/**
		 *\~english
		 *\brief		Renders the given 2D texture.
		 *\param[in]	size		The render viewport size.
		 *\param[in]	texture		The texture.
		 *\param[in]	pipeline	The pipleline used to render the texture.
		 *\param[in]	matrixUbo	The uniform buffer receiving matrices.
		 *\~french
		 *\brief		Dessine la texture 2D donnée.
		 *\param[in]	size		La taille du viewport de rendu.
		 *\param[in]	texture		La texture.
		 *\param[in]	pipeline	Le pipeline utilisé pour dessiner la texture.
		 *\param[in]	matrixUbo	Le tampon d'uniformes recevant les matrices.
		 */
		inline void renderTexture( castor::Size const & size
			, TextureLayout const & texture
			, RenderPipeline & pipeline
			, MatrixUbo & matrixUbo )
		{
			static castor::Position const position;
			renderTexture( position
				, size
				, texture
				, pipeline
				, matrixUbo );
		}
Beispiel #14
0
void LLDrawPoolGlow::render(S32 pass)
{
	S32 mode = gViewerWindow->getMaskMode();
	LLFastTimer t(FTM_RENDER_GLOW);
	LLGLEnable blend(GL_BLEND);
	LLGLDisable test(GL_ALPHA_TEST);
	gGL.flush();
	/// Get rid of z-fighting with non-glow pass.
	LLGLEnable polyOffset(GL_POLYGON_OFFSET_FILL);
	glPolygonOffset(-1.0f, -1.0f);
	gGL.setSceneBlendType(LLRender::BT_ADD);
	
	U32 shader_level = LLViewerShaderMgr::instance()->getVertexShaderLevel(LLViewerShaderMgr::SHADER_OBJECT);

	if (shader_level > 0 && fullbright_shader)
	{
		fullbright_shader->bind();
	}
	else
	{
		gPipeline.enableLightsFullbright(LLColor4(1,1,1,1));
	}

	LLGLDepthTest depth(GL_TRUE, GL_FALSE);
	gGL.setColorMask(false, true);
	if (shader_level > 1)
	{
		pushBatches(LLRenderPass::PASS_GLOW, getVertexDataMask() | LLVertexBuffer::MAP_TEXTURE_INDEX, TRUE, TRUE);
	}
	else
	{
		renderTexture(LLRenderPass::PASS_GLOW, getVertexDataMask());
	}
	
	if(mode == MASK_MODE_RIGHT)
	{
	gGL.setColorMask(false,true,true,false);
	}
	if(mode == MASK_MODE_LEFT)
	{
	gGL.setColorMask(true,false,false,false);
	}
	if(mode == MASK_MODE_NONE)
	{
	gGL.setColorMask(true, false);
	}
	gGL.setSceneBlendType(LLRender::BT_ALPHA);
	
	if (shader_level > 0 && fullbright_shader)
	{
		fullbright_shader->unbind();
	}
}
void MTexturePixmapPrivate::paint(QPainter *painter)
{
    if (direct_fb_render) {
        glBindTexture(GL_TEXTURE_2D, 0);
        return;
    }

#ifdef GLES2_VERSION
    if (painter->paintEngine()->type() != QPaintEngine::OpenGL2)
        return;
    if (current_window_group.isNull()) 
        renderTexture(painter->combinedTransform());
#else
    if (painter->paintEngine()->type() != QPaintEngine::OpenGL2
        && painter->paintEngine()->type() != QPaintEngine::OpenGL)
        return;
    painter->beginNativePainting();
    renderTexture(painter->combinedTransform());
    painter->endNativePainting();
#endif
}
void draw_score(SDL_Renderer* ren)
{
    if(scoreInBuffer != score) {
        sprintf(score_buffer, "%lu", score);
        scoreInBuffer = score;
    }
    SDL_Texture *font_image = renderText(score_buffer, font, score_color, ren);
    if (font_image == NULL){
        exit(1);
    }            
        renderTexture(font_image, ren, score_x, score_y);
}
Beispiel #17
0
bool Renderer::renderTexture(SDL_Texture* texture, bool scale) {
	if (scale) {
		return (SDL_RenderCopy(Renderer::g_renderer, texture, NULL, NULL) == 0);
	}
	else {
		int wTexture;
		int hTexture;
		if (SDL_QueryTexture(texture, NULL, NULL, &wTexture, &hTexture) < 0)
			return false;
		return renderTexture(texture, 0, 0, 0, 0, wTexture, hTexture);
	}
}
Beispiel #18
0
/*
 * Draw an SDL_Texture to an SDL_Renderer at position x, y, preserving
 * the texture's width and height and taking a clip of the texture if desired
 * If a clip is passed, the clip's width and height will be used instead of the texture's
 * @param tex The source texture we want to draw
 * @param rend The renderer we want to draw too
 * @param x The x coordinate to draw too
 * @param y The y coordinate to draw too
 * @param clip The sub-section of the texture to draw (clipping rect)
 *		default of nullptr draws the entire texture
 */
void renderTexture(SDL_Texture *tex, SDL_Renderer *ren, int x, int y, SDL_Rect *clip = nullptr){
	SDL_Rect dst;
	dst.x = x;
	dst.y = y;
	if (clip != nullptr){
		dst.w = clip->w;
		dst.h = clip->h;
	}
	else {
		SDL_QueryTexture(tex, NULL, NULL, &dst.w, &dst.h);
	}
	renderTexture(tex, ren, dst, clip);
}
Beispiel #19
0
	void Sprite::renderSprite()
	{
		float width = getWidth();
		float height = getHeight();
		
		//glBindTexture(GL_TEXTURE_2D, mAsset->getTexture()->getTextureId());
		mAsset->getTexture()->bindTexture();

		const TextureWindow &win = mAsset->getAnimationWindows()[mCurrentFrame];

		renderTexture(win, width, height);
		//glBindTexture(GL_TEXTURE_2D, 0);
	}
void LLDrawPoolSimple::render(S32 pass)
{
	LLGLDisable blend(GL_BLEND);
	LLGLState alpha_test(GL_ALPHA_TEST, gPipeline.canUseWindLightShadersOnObjects());
	gGL.setAlphaRejectSettings(LLRender::CF_GREATER, 0.5f);

	{ //render simple
		LLFastTimer t(LLFastTimer::FTM_RENDER_SIMPLE);
		gPipeline.enableLightsDynamic();
		renderTexture(LLRenderPass::PASS_SIMPLE, getVertexDataMask());
	}

	{
		LLFastTimer t(LLFastTimer::FTM_RENDER_GRASS);
		LLGLEnable test(GL_ALPHA_TEST);
		LLGLEnable blend(GL_BLEND);
		gGL.setSceneBlendType(LLRender::BT_ALPHA);
		//render grass
		LLRenderPass::renderTexture(LLRenderPass::PASS_GRASS, getVertexDataMask());
	}			

	{ //render fullbright
		if (mVertexShaderLevel > 0)
		{
			fullbright_shader->bind();
			fullbright_shader->uniform1f(LLViewerShaderMgr::FULLBRIGHT, 1.f);
		}
		else
		{
			gPipeline.enableLightsFullbright(LLColor4(1,1,1,1));
		}
		LLFastTimer t(LLFastTimer::FTM_RENDER_FULLBRIGHT);
		U32 fullbright_mask = LLVertexBuffer::MAP_VERTEX | LLVertexBuffer::MAP_TEXCOORD | LLVertexBuffer::MAP_COLOR;
		renderTexture(LLRenderPass::PASS_FULLBRIGHT, fullbright_mask);
	}

	gGL.setAlphaRejectSettings(LLRender::CF_DEFAULT);
}
Beispiel #21
0
void render_background(SDL_Renderer *renderer)
{
	int x, y, h, w;
	SDL_QueryTexture(background, NULL, NULL, &w, &h);
	x = background_i;
	y = 0;

	for (; x <= 2000; x += w) {
		renderTexture(background, renderer, x, y);
	}

	if (background_i > 1000)
		background_i = 0;
}
Beispiel #22
0
void COpenGL::transformScreenToDisplay()
{
	glEnable(GL_TEXTURE_2D);
	// Set up an array of values to use as the sprite vertices.
	GLfloat vertices[] =
	{
		0, 0,
		1, 0,
		1, 1,
		0, 1,
	};

	// Set up an array of values for the texture coordinates.
	GLfloat texcoords[] =
	{
		0, 0,
		1, 0,
		1, 1,
		0, 1,
	};

	//Render the vertices by pointing to the arrays.
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

	glVertexPointer(2, GL_FLOAT, 0, vertices);
	glTexCoordPointer(2, GL_FLOAT, 0, texcoords);

	glEnable(GL_BLEND);

    loadSurface(m_texture, *mpScreenSfc.getSDLSurface());
	renderTexture(m_texture);

	glDisableClientState(GL_VERTEX_ARRAY);
	glDisableClientState(GL_TEXTURE_COORD_ARRAY);
	glDisable(GL_BLEND);
	glDisable(GL_TEXTURE_2D);

	gInput.renderOverlay();

#if SDL_VERSION_ATLEAST(2, 0, 0)
    SDL_GL_SwapWindow(window);
#else
    SDL_GL_SwapBuffers();
#endif

    clearSurfaces();
}
Beispiel #23
0
void CanvasGL::renderLayer(size_t idx) {
    previousRenderedLayerIdx_ = idx;
    if (imageGL_) {
        const LayerGL* layerGL = imageGL_->getLayerGL(layerType_,idx);
        if (layerGL) {
            TextureUnit textureUnit;
            layerGL->bindTexture(textureUnit.getEnum());
            renderTexture(textureUnit.getUnitNumber());
            layerGL->unbindTexture();
            return;
        } else {
            renderNoise();
        }
    }
    if (!image_) renderNoise();
}
Beispiel #24
0
		void Renderer::renderTileMap(novus::world::TileMap* tileMap) {
			sf::Texture* sfTexture = renderUtils->getTileset(tileMap->tileset);
			sf::IntRect viewport = renderUtils->getRenderWindow()->getViewport(renderUtils->getRenderWindow()->getView());
			sf::IntRect dRect (0, 0, 32, 32);
			for (int y = 0; y < tileMap->height; y++) {
				for (int x = 0; x < tileMap->width; x++) {
					dRect.left = x * 32;
					dRect.top = y * 32;
					if (viewport.intersects(dRect)) {
						novus::world::Tile* tile = &tileMap->tiles[y * tileMap->width + x];
						if (tile->texture < 0) { continue; }
						sf::IntRect sRect = sf::IntRect(tile->texture % 8 * 32, tile->texture / 8 * 32, 32, 32);
						renderTexture(sfTexture, dRect, sRect);
					}
				}
			}
		}
Beispiel #25
0
void LLDrawPoolFullbright::render(S32 pass)
{ //render fullbright
	LLFastTimer t(FTM_RENDER_FULLBRIGHT);
	gGL.setSceneBlendType(LLRender::BT_ALPHA);

	if (mVertexShaderLevel > 0)
	{
		fullbright_shader->bind();
		fullbright_shader->uniform1f(LLViewerShaderMgr::FULLBRIGHT, 1.f);
		U32 fullbright_mask = LLVertexBuffer::MAP_VERTEX | LLVertexBuffer::MAP_TEXCOORD0 | LLVertexBuffer::MAP_COLOR | LLVertexBuffer::MAP_TEXTURE_INDEX;
		pushBatches(LLRenderPass::PASS_FULLBRIGHT, fullbright_mask, TRUE, TRUE);
	}
	else
	{
		gPipeline.enableLightsFullbright(LLColor4(1,1,1,1));
		U32 fullbright_mask = LLVertexBuffer::MAP_VERTEX | LLVertexBuffer::MAP_TEXCOORD0 | LLVertexBuffer::MAP_COLOR;
		renderTexture(LLRenderPass::PASS_FULLBRIGHT, fullbright_mask);
	}
}
Beispiel #26
0
void Sprite::render(int cameraoffset_x, int cameraoffset_y, float zoom, int height)
{
  float pixel_x = 0;
  float pixel_y = 0;
  pixel_y = getPixelY(getPosX(), getPosY(), cameraoffset_x, cameraoffset_y, zoom, mScale) - height;
  pixel_x = getPixelX(getPosX(), getPosY(), cameraoffset_x, cameraoffset_y, zoom, mScale);
  
  //Render only whats visible:
  const int offscreen_tolerance = 3*TILE_SIZE*zoom;
  int screen_width;
  int screen_height;
  SDL_GetWindowSize(mWindow, &screen_width, &screen_height);

  if ((pixel_x >= 0-offscreen_tolerance) or
      (pixel_x+TILE_SIZE*zoom <= screen_width+offscreen_tolerance) or
      (pixel_y >= 0-offscreen_tolerance) or
      (pixel_y+TILE_SIZE*zoom <= screen_height+offscreen_tolerance))
    {
      renderTexture(mTexture, mRenderer, pixel_x, pixel_y, mScale*zoom, mScale*zoom);
    }
}
void render(Game *g)
{        
	g->mouseThrustOn=false;    
	glClear(GL_COLOR_BUFFER_BIT);
	if(intro < 1) {
		char screen;
		if (selected_screen == LEFT){
			screen = 'L';
		}
		else{
			screen = 'R';
		}
		if (hud->isShowWelcome() == true){
			hud->showIntro(screen,introTexture, bgTexture1, bgTexture2);
			if (hud->getAI() == true){
				hud->selectAI();
			}
			else{
				hud->selectHuman();
			}
			return;
		}
		else if (hud->isShowHelpMenu() == true){
			hud->showHelpMenu(helpMenuTexture);
			return;
		}
		return;
	}
	else{
		renderTexture(bgTexture, xres, yres);
	}

	if (is_gameover){
		renderTexture(gameOverTexture, xres, yres);
		high_score = setHighScore(ball.getPlayer1Score(), ball.getPlayer2Score());
		//cout << "Final Score : " << high_score << "\n";
		hud->showGameOver(high_score,ball.getPlayer1Score(), ball.getPlayer2Score());
		return;
	}

	hud->showScore(ball.getPlayer1Score(), ball.getPlayer2Score());
	hud->showHealth(hud->getPlayer1Health(), hud->getPlayer2Health());
	hud->showCourtYard();

	//DRAW BOMB:
	if (level == 1){

		GLuint which_bomb_texture = bombTexture;
		if ((bombBegin + 2) > time(NULL)){
			which_bomb_texture = explodeTexture;
		}
		else{
			which_bomb_texture = bombTexture;
		}

		portal0.render(portalTexture0);
		portal1.render(portalTexture1);


		hud->renderBomb(which_bomb_texture,bomb_posx,bomb_posy,bomb_width,bomb_height);
	}

	//Draw the paddle
	glColor3f(0.0, 0.5, 0.5);
	paddle1.render();
	glColor3f(0.7, 0.5, 0.0);
	paddle2.render();
	glEnd();
	//Draw the ball
	ball.render();

	//If option 2 is selected i.e. ninja_robot.ppm is the background
	//Level 2 selected
	//Draw some obstacles to showcase a difference between level 1 and level2
	if(level == 2) {
		obstacle->render();
	}
	hud->showTimer(getTimer());
	if (hud->isPaused()){
		hud->showPaused(pausedTexture);
	}
}
Beispiel #28
0
int main(int argc, char **argv){
	if (SDL_Init(SDL_INIT_EVERYTHING) != 0){
		logSDLError(std::cout, "SDL_Init");
		return 1;
	}

	if (TTF_Init() != 0){
	logSDLError(std::cout, "TTF_Init");
	return 1;
	}

	SDL_Window *window = SDL_CreateWindow("Hello World!", 100, 100, SCREEN_WIDTH, GRID_HEIGHT + HUD_HEIGHT,
	SDL_WINDOW_SHOWN);
	if (window == nullptr){
		logSDLError(std::cout, "CreateWindow");
		SDL_Quit();
		return 2;
	}

	SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
	if (renderer == nullptr){
		logSDLError(std::cout, "CreateRenderer");
		SDL_DestroyWindow(window);
		SDL_Quit();
		return 3;
	}

	//The textures we'll be using
	SDL_Texture *background = loadTexture("res/game_bg.jpg", renderer);
	SDL_Texture *image = loadTexture("res/Blue.png", renderer);
	SDL_Texture *blueGem = loadTexture("res/Blue.png", renderer);
	SDL_Texture *greenGem = loadTexture("res/Green.png", renderer);
	SDL_Texture *purpleGem = loadTexture("res/Purple.png", renderer);
	SDL_Texture *redGem = loadTexture("res/Red.png", renderer);
	SDL_Texture *yellowGem = loadTexture("res/Yellow.png", renderer);
	SDL_Texture *gems[] = {blueGem, greenGem, purpleGem, redGem, yellowGem};
	SDL_Texture *cursorYellowImg = loadTexture("res/cursor_yellow.png", renderer);
	SDL_Texture *cursorGreenImg = loadTexture("res/cursor_green.png", renderer);
	//Make sure they both loaded ok
	if (background == nullptr || image == nullptr){
		SDL_DestroyRenderer(renderer);
		SDL_DestroyWindow(window);
		SDL_Quit();
		return 4;
	}

	//We'll render the string "TTF fonts are cool!" in white
	//Color is in RGB format
	SDL_Color color = { 255, 255, 255 };
	SDL_Texture *textImage;
	TTF_Font* font = loadFont("res/sample.ttf", 32);

	//Our event structure
	SDL_Event e;
	//For tracking if we want to quit
	bool quit = false;
	int gameState = GAMESTATE_TITLE_SCREEN;
	int** pGridArray;
	gem* pGemsArray = new gem[NUM_ROWS * NUM_COLUMNS];

	int score;

	int mouseX = 0;
	int mouseY = 0;
	int numGemsSelected = 0;
	int selectedGems[2][2] = {{-1, -1}, {-1, -1}};
	bool gemSelected = false;
	int cursorRow = 0;
	int cursorColumn = 0;
	bool foundMatch = false;
	int thisRow, thisColumn;
	bool gemsDropping;
	bool cancelSwap = false;
	bool mouseDown = false;
	int dragStartX;
	int dragStartY;
	bool dragStarted = false;
	time_t timer;
	time_t startTime;
	std::ostringstream os;

	while (!quit)
	{
		switch (gameState)
		{
			case GAMESTATE_TITLE_SCREEN:
			break;

			case GAMESTATE_INIT:
				for (int i = 0; i < NUM_ROWS*NUM_COLUMNS; i++)
				{
					pGemsArray[i].state = GEMSTATE_IDLE;
				}
				pGridArray = initGrid(pGemsArray);
				score = 0;
				startTime = time(0);
				gameState = GAMESTATE_INITIAL_CHECK_MATCH;
			break;

			case GAMESTATE_INITIAL_CHECK_MATCH:
				SDL_Delay(1000);
				checkMatchAllRows(pGridArray, pGemsArray, &score);
				gameState = GAMESTATE_CHECKDROP;
			break;

			case GAMESTATE_CHECK_MATCH:

				foundMatch = false;
				for (int row = NUM_ROWS-1; row >= 0; row--)
				{
					for (int column = 0; column < NUM_COLUMNS; column++)
					{
						if (pGemsArray[pGridArray[row][column]].prevState == GEMSTATE_FALL)
						{
							foundMatch = checkMatchAfterSwap(pGridArray, pGemsArray, &pGemsArray[pGridArray[row][column]], &score);
						}
					}
				}

				gameState = GAMESTATE_CHECKDROP;

				for (int row = NUM_ROWS-1; row >= 0; row--)
				{
					for (int column = 0; column < NUM_COLUMNS; column++)
					{
						pGemsArray[pGridArray[row][column]].prevState = -1;
					}
				}

			break;

			case GAMESTATE_AWAIT_INPUT:

				if (gemSelected)
				{
					if (numGemsSelected < 2)
					{
						numGemsSelected++;
					}
					else
					{
						numGemsSelected = 0;
					}
					cursorRow = (mouseY-HUD_HEIGHT) / ROW_HEIGHT;
					cursorColumn = mouseX / COLUMN_WIDTH;

					// Store the location of the selected gem
					selectedGems[numGemsSelected-1][ROW] = cursorRow;
					selectedGems[numGemsSelected-1][COLUMN] = cursorColumn; 

					gemSelected = false;

					// Check if ready to swap
					if (numGemsSelected == 2)
					{
						int state = checkSwapGems((int*)selectedGems);
						state = setSwapGems(state, pGemsArray, pGridArray, selectedGems[0][ROW], selectedGems[0][COLUMN], selectedGems[1][ROW], selectedGems[1][COLUMN]);
						if (state == -1)
						{

							// Remove all cursors
							numGemsSelected = 0;

							// Set new cursor to this position
							cursorRow = (mouseY-HUD_HEIGHT) / ROW_HEIGHT;
							cursorColumn = mouseX / COLUMN_WIDTH;

							// Store the location of the selected gem
							selectedGems[numGemsSelected-1][ROW] = cursorRow;
							selectedGems[numGemsSelected-1][COLUMN] = cursorColumn; 
							gemSelected = true;
						}
						else
						{
							gameState = state;
						}
					}
				}
				
			break;


			case GAMESTATE_SWAP_LEFT:
			case GAMESTATE_SWAP_RIGHT:

				thisRow = selectedGems[0][ROW];
				if (gameState == GAMESTATE_SWAP_LEFT)
					thisColumn = selectedGems[0][COLUMN];
				else
					thisColumn = selectedGems[1][COLUMN];

				pGemsArray[pGridArray[thisRow][thisColumn]].x += pGemsArray[pGridArray[thisRow][thisColumn]].velocity;
				pGemsArray[pGridArray[thisRow][thisColumn-1]].x += pGemsArray[pGridArray[thisRow][thisColumn-1]].velocity;
				
				if (pGemsArray[pGridArray[thisRow][thisColumn]].x <= -COLUMN_WIDTH)
				{
					numGemsSelected = 0;

					// Swap with other gem
					int temp = pGridArray[thisRow][thisColumn];
					pGridArray[thisRow][thisColumn] = pGridArray[thisRow][thisColumn-1];
					pGridArray[thisRow][thisColumn-1] = temp;

					pGemsArray[pGridArray[thisRow][thisColumn]].velocity = 0;
					pGemsArray[pGridArray[thisRow][thisColumn]].x = 0;
					pGemsArray[pGridArray[thisRow][thisColumn]].column++;
					pGemsArray[pGridArray[thisRow][thisColumn]].state = GEMSTATE_IDLE;
					
					pGemsArray[pGridArray[thisRow][thisColumn-1]].velocity = 0;
					pGemsArray[pGridArray[thisRow][thisColumn-1]].x = 0;
					pGemsArray[pGridArray[thisRow][thisColumn-1]].column--;
					pGemsArray[pGridArray[thisRow][thisColumn-1]].state = GEMSTATE_IDLE;

					bool foundMatch = false;
					bool foundMatch2 = false;
					if (!cancelSwap)
					{
						foundMatch = checkMatchAfterSwap(pGridArray, pGemsArray, &pGemsArray[pGridArray[thisRow][thisColumn]], &score);
						foundMatch2 = checkMatchAfterSwap(pGridArray, pGemsArray, &pGemsArray[pGridArray[thisRow][thisColumn-1]], &score);
					}

					if (foundMatch || foundMatch2)
					{
						gameState = GAMESTATE_CHECKDROP;
					}
					else
					{
						if (cancelSwap)
						{
							// Gems have gone back to their original positions after cancelling the swap
							cancelSwap = false;
							gameState = GAMESTATE_AWAIT_INPUT;
						}
						else
						{
							// Swap the gems back to their original positions
							gameState = setSwapGems(gameState, pGemsArray, pGridArray, selectedGems[0][ROW], selectedGems[0][COLUMN], selectedGems[1][ROW], selectedGems[1][COLUMN]);
							cancelSwap = true;
						}
	
					}
				}					
			break;

			case GAMESTATE_SWAP_UP:
			case GAMESTATE_SWAP_DOWN:

				thisColumn = selectedGems[0][COLUMN];
				if (gameState == GAMESTATE_SWAP_UP)
					thisRow = selectedGems[0][ROW];
				else
					thisRow = selectedGems[1][ROW];

				pGemsArray[pGridArray[thisRow][thisColumn]].y += pGemsArray[pGridArray[thisRow][thisColumn]].velocity;
				pGemsArray[pGridArray[thisRow-1][thisColumn]].y += pGemsArray[pGridArray[thisRow-1][thisColumn]].velocity;
				
				if (pGemsArray[pGridArray[thisRow][thisColumn]].y <= -ROW_HEIGHT)
				{
					numGemsSelected = 0;
					
					// Swap with other gem
					int temp = pGridArray[thisRow][thisColumn];
					pGridArray[thisRow][thisColumn] = pGridArray[thisRow-1][thisColumn];
					pGridArray[thisRow-1][thisColumn] = temp;

					pGemsArray[pGridArray[thisRow][thisColumn]].velocity = 0;
					pGemsArray[pGridArray[thisRow][thisColumn]].y = 0;
					pGemsArray[pGridArray[thisRow][thisColumn]].row++;
					pGemsArray[pGridArray[thisRow][thisColumn]].state = GEMSTATE_IDLE;

					pGemsArray[pGridArray[thisRow-1][thisColumn]].velocity = 0;
					pGemsArray[pGridArray[thisRow-1][thisColumn]].y = 0;
					pGemsArray[pGridArray[thisRow-1][thisColumn]].row--;
					pGemsArray[pGridArray[thisRow-1][thisColumn]].state = GEMSTATE_IDLE;

					bool foundMatch = false;
					bool foundMatch2 = false;
					if (!cancelSwap)
					{
						foundMatch = checkMatchAfterSwap(pGridArray, pGemsArray, &pGemsArray[pGridArray[thisRow][thisColumn]], &score);
						foundMatch2 = checkMatchAfterSwap(pGridArray, pGemsArray, &pGemsArray[pGridArray[thisRow-1][thisColumn]], &score);
					}

					if (foundMatch || foundMatch2)
					{
						gameState = GAMESTATE_CHECKDROP;
					}
					else
					{					
						if (cancelSwap)
						{
							// Gems have gone back to their original positions after cancelling the swap
							cancelSwap = false;
							gameState = GAMESTATE_AWAIT_INPUT;
						}
						else
						{
							// Swap the gems back to their original positions
							gameState = setSwapGems(gameState, pGemsArray, pGridArray, selectedGems[0][ROW], selectedGems[0][COLUMN], selectedGems[1][ROW], selectedGems[1][COLUMN]);
							cancelSwap = true;
						}
					}
				}					
			break;

			case GAMESTATE_CHECKDROP:

				if (checkDrop(pGridArray, pGemsArray))
				{
					gameState = GAMESTATE_DROP;
				}
				else
				{
					gameState = GAMESTATE_ADD_GEMS;
				}

			break;

			case GAMESTATE_DROP:

				// Check if any more gems are dropping
				gemsDropping = false;
				for (int i = 0; i < NUM_ROWS * NUM_COLUMNS; i++)
				{
					if (pGemsArray[i].state == GEMSTATE_FALL || pGemsArray[i].state == GEMSTATE_ENTER || pGemsArray[i].state == GEMSTATE_BOUNCE)
					{
						gemsDropping = true;
						break;
					}
				}

				if (!gemsDropping)
				{
					gameState = GAMESTATE_CHECK_MATCH;
				}
			break;


			case GAMESTATE_GAME_OVER:

				if (timer - startTime >= GAME_OVER_TIMEOUT)
				{
					gameState = GAMESTATE_TITLE_SCREEN;
					startTime = time(0);
				}
			break;

			case GAMESTATE_ADD_GEMS:

				srand(time(NULL));
				bool gemsAdded = false;
				// Find empty columns
				for (int column = 0; column < NUM_COLUMNS; column++)
				{
					// Find empty spaces in this column starting from top
					int row = 0;
					
					while (pGemsArray[pGridArray[row][column]].type == -1)
					{
						pGemsArray[pGridArray[row][column]].type = rand() % NUM_GEM_TYPES;
						pGemsArray[pGridArray[row][column]].state = GEMSTATE_ENTER;
						pGemsArray[pGridArray[row][column]].row = 0;
						pGemsArray[pGridArray[row][column]].column = column;
						pGemsArray[pGridArray[row][column]].y = -(ROW_HEIGHT+10)*(NUM_ROWS - row);
						pGemsArray[pGridArray[row][column]].velocity = 1;
						pGemsArray[pGridArray[row][column]].dropToRow = row;
						pGemsArray[pGridArray[row][column]].prevState = GEMSTATE_FALL;
						row++;
						gemsAdded = true;
					}
				}

				numGemsSelected = 0;
				if (gemsAdded)
				{
					gameState = GAMESTATE_DROP;
				}
				else
				{
					gameState = GAMESTATE_AWAIT_INPUT;
				}
			break;

		}

		if (gameState != GAMESTATE_TITLE_SCREEN)
		{

			for (int row = NUM_ROWS-1; row >= 0; row--)
			{
				for (int column = 0; column < NUM_COLUMNS; column++)
				{
					gem* thisGem = &pGemsArray[pGridArray[row][column]];

					switch (pGemsArray[pGridArray[row][column]].state)
					{
						case GEMSTATE_IDLE:
						break;

						case GEMSTATE_FALL:
						case GEMSTATE_ENTER:

							if ((pGemsArray[pGridArray[row][column]].type != -1 && pGemsArray[pGridArray[row+1][column]].type == -1) ||
								pGemsArray[pGridArray[row][column]].state == GEMSTATE_ENTER)
							{
								dropGem(pGridArray, &pGemsArray[pGridArray[row][column]], pGemsArray);
							}
						break;

						case GEMSTATE_BOUNCE:

							thisGem->y += thisGem->velocity;
							thisGem->velocity--;		

							if (thisGem->y <= 0)
							{
								thisGem->y = 0;
								thisGem->velocity = 0;
								thisGem->state = GEMSTATE_IDLE;
							}
						break;
					}	
				}
			}
		}
	

		timer = time(0);

		if (gameState != GAMESTATE_TITLE_SCREEN && gameState != GAMESTATE_GAME_OVER && timer - startTime >= TIMEOUT)
		{
			gameState = GAMESTATE_GAME_OVER;
			startTime = time(0);
		}

		//Read user input & handle it
		//Read any events that occured, for now we'll just quit if any event occurs
		while (SDL_PollEvent(&e)){
			//If user closes the window
			if (e.type == SDL_QUIT){
				quit = true;
			}
			//If user clicks the mouse
			if (e.type == SDL_MOUSEBUTTONDOWN){
				if (gameState == GAMESTATE_TITLE_SCREEN)
				{
					gameState = GAMESTATE_INIT;
				}
				else
				{
					mouseX = e.button.x;
					mouseY = e.button.y;
					mouseDown = true;
					dragStartX = mouseX;
					dragStartY = mouseY;
					dragStarted = false;
				}
			}
			if (e.type == SDL_MOUSEBUTTONUP){
				if (gameState != GAMESTATE_TITLE_SCREEN)
				{
					mouseDown = false;
					gemSelected = true;
				}
			}
			if (e.type == SDL_MOUSEMOTION && mouseDown)
			{
				if (e.motion.xrel < 0)		// Dragged left
				{
					if (mouseX - e.motion.x > DRAG_DEAD_ZONE && !dragStarted)
					{
						// Start a new selection
						numGemsSelected = 0;
						dragStarted = true;
						gemSelected = true;
					}				
					 else if (mouseX - e.motion.x > DRAG_DEAD_ZONE && dragStarted)
					{
						// Select next gem
						mouseX -= COLUMN_WIDTH;
						mouseDown = false;
						gemSelected = true;
					}
				}
				else if (e.motion.xrel > 0)		// Dragged right
				{
					if (e.motion.x - mouseX > DRAG_DEAD_ZONE && !dragStarted)
					{
						numGemsSelected = 0;
						dragStarted = true;
						gemSelected = true;
					}				
					 else if (e.motion.x - mouseX > DRAG_DEAD_ZONE && dragStarted)
					{
						mouseX += COLUMN_WIDTH;
						mouseDown = false;
						gemSelected = true;
					}
				}
				else if (e.motion.yrel < 0)		// Dragged up
				{
					if (mouseY - e.motion.y > DRAG_DEAD_ZONE && !dragStarted)
					{
						numGemsSelected = 0;
						dragStarted = true;
						gemSelected = true;
					}				
					 else if (mouseY - e.motion.y > DRAG_DEAD_ZONE && dragStarted)
					{
						mouseY -= ROW_HEIGHT;
						mouseDown = false;
						gemSelected = true;
					}
				}
				else if (e.motion.yrel > 0)		// Dragged down
				{
					if (e.motion.y - mouseY > DRAG_DEAD_ZONE && !dragStarted)
					{
						numGemsSelected = 0;
						dragStarted = true;
						gemSelected = true;
					}				
					 else if (e.motion.y - mouseY > DRAG_DEAD_ZONE && dragStarted)
					{
						mouseY += ROW_HEIGHT;
						mouseDown = false;
						gemSelected = true;
					}
				}
			}
		}

		//Render scene

		//First clear the renderer
		SDL_RenderClear(renderer);

		//Get the width and height from the texture so we know how much to move x,y by
		//to tile it correctly
		int bW, bH;
		SDL_QueryTexture(background, NULL, NULL, &bW, &bH);

		int x;
		int y;
		//Draw the background
		renderTexture(background, renderer, 0, 0);

		if (gameState != GAMESTATE_TITLE_SCREEN && gameState != GAMESTATE_INIT)
		{
			// Draw the gems
			// int rowHeight = GRID_HEIGHT / NUM_ROWS;
			int gemWidth, gemHeight;
			SDL_QueryTexture(gems[0], NULL, NULL, &gemWidth, &gemHeight);

			// Draw cursors
			if (numGemsSelected > 0)
			{
				int cursorWidth, cursorHeight;
				SDL_QueryTexture(cursorYellowImg, NULL, NULL, &cursorWidth, &cursorHeight);
				renderTexture(cursorYellowImg, renderer, 
					selectedGems[0][COLUMN] * COLUMN_WIDTH + COLUMN_WIDTH/2 - cursorWidth/2, 
					selectedGems[0][ROW] * ROW_HEIGHT + ROW_HEIGHT/2 - cursorHeight/2 + HUD_HEIGHT);
			
				if (numGemsSelected == 2)
				{
					renderTexture(cursorGreenImg, renderer, 
						selectedGems[1][COLUMN] * COLUMN_WIDTH + COLUMN_WIDTH/2 - cursorWidth/2, 
						selectedGems[1][ROW] * ROW_HEIGHT + ROW_HEIGHT/2 - cursorHeight/2 + HUD_HEIGHT);
				}
			}

			for (int i = 0; i < NUM_ROWS * NUM_COLUMNS; i++)
			{
				if (pGemsArray[i].type != -1)
				{
					x = COLUMN_WIDTH * pGemsArray[i].column + COLUMN_WIDTH/2 - gemWidth/2;
					y = ROW_HEIGHT * pGemsArray[i].row + ROW_HEIGHT/2 - gemHeight/2 + HUD_HEIGHT;
					renderTexture(gems[pGemsArray[i].type], renderer, x + pGemsArray[i].x, y + pGemsArray[i].y);
				}
			}
		}

		
		// Draw text
		int iW, iH;
		switch (gameState)
		{
			case GAMESTATE_TITLE_SCREEN:
		
			os.str("");
			os.clear();
			os << "Click mouse to start";

			textImage = renderText(os.str(), font, color, renderer);
			if (textImage == nullptr){
				return 1;
			}

			SDL_QueryTexture(textImage, NULL, NULL, &iW, &iH);
			SDL_QueryTexture(textImage, NULL, NULL, &iW, &iH);
			x = SCREEN_WIDTH/2 - iW/2;
			y = GRID_HEIGHT/2;
			renderTexture(textImage, renderer, x, y);

			break;

			case GAMESTATE_GAME_OVER:

				os.str("");
				os.clear();
				os << "GAME OVER. Your score is " << score;

				textImage = renderText(os.str(), font, color, renderer);
				if (textImage == nullptr){
					return 1;
				}

				SDL_QueryTexture(textImage, NULL, NULL, &iW, &iH);
				SDL_QueryTexture(textImage, NULL, NULL, &iW, &iH);
				x = SCREEN_WIDTH/2 - iW/2;
				y = GRID_HEIGHT/2;
				renderTexture(textImage, renderer, x, y);

			break;

			default:

				os.str("");
				os.clear();
				os << "Score " << score;

				textImage = renderText(os.str(), font, color, renderer);
				if (textImage == nullptr){
					return 1;
				}
				x = 10;
				y = 10;
				renderTexture(textImage, renderer, x, y);
				// drawText("Score ", score, renderer, textImage, font, color, 10, 10);


				os.str("");
				os.clear();
				os << "Time " << (TIMEOUT - (timer - startTime));
				textImage = renderText(os.str(), font, color, renderer);
				if (textImage == nullptr){
					return 1;
				}
				//Get the texture w/h so we can position it correctly on the screen
				SDL_QueryTexture(textImage, NULL, NULL, &iW, &iH);
				SDL_QueryTexture(textImage, NULL, NULL, &iW, &iH);
				x = SCREEN_WIDTH -10 - iW;
				renderTexture(textImage, renderer, x, y);

				// drawText seems to always draw to the left of the screen so commented out
				// drawText("Time ", timer - startTime, renderer, textImage, font, color, 100, 10);

			break;
		}

		//Update the screen
		SDL_RenderPresent(renderer);
	}

	//Clean up our objects and quit
	SDL_DestroyTexture(background);
	SDL_DestroyTexture(image);
	SDL_DestroyTexture(blueGem);
	SDL_DestroyTexture(greenGem);
	SDL_DestroyTexture(purpleGem);
	SDL_DestroyTexture(redGem);
	SDL_DestroyTexture(yellowGem);

	for (int i = 0; i < sizeof(*gems); i++)
	{
		gems[i] = NULL;
	}

	for (int row = 0; row < NUM_ROWS; row++)
	{
		delete [] pGridArray[row];
	}
	delete [] pGridArray;

	delete [] pGemsArray;

	SDL_DestroyRenderer(renderer);
	SDL_DestroyWindow(window);
	SDL_Quit();

	return 0;
}
Beispiel #29
0
int main (void) { //int argv, char **argc
	int x, y, i, j, bW, bH, fW, fH, sW, sH;
	int curClip = 0;
	bool quit = 0;
	const Uint8 *kbState = SDL_GetKeyboardState(NULL);
	char **imgFromSrc;

	SDL_Event e;
	SDL_Window *win;
	SDL_Renderer *ren;
	SDL_Texture *bg;
	SDL_Texture *fg;

	SDL_Rect wall;
	SDL_Rect clips[CLIPS_AMOUNT];

	if (SDL_Init(SDL_INIT_VIDEO) != 0 || (IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG) != IMG_INIT_PNG) {
		logSDLError("Init");
		return 1;
	}

	win = SDL_CreateWindow("Hello World!", WINDOW_OFFSET_X, WINDOW_OFFSET_Y, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN);
	if (win == NULL) {
		logSDLError("CreateWindow");
		return 2;
	}

	ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
	if (ren == NULL) {
		logSDLError("CreateRenderer");
		return 3;
	}

	imgFromSrc = bmp_xpm;
	bg = loadHeader(imgFromSrc, ren);
	fg = loadTexture("../SDL/sprites/sprite.png", ren);
	if (bg == NULL || fg == NULL) {
		logSDLError("loadTexture");
		return 4;
	}

	SDL_RenderClear(ren);

	SDL_QueryTexture(bg, NULL, NULL, &bW, &bH);

	bW /= 2;
	bH /= 2;

	for (i = 0; i < WINDOW_WIDTH / bW; i++) {
		for (j = 0; j <= WINDOW_HEIGHT / bH; j++) {
			renderTextureS(bg, ren, i * bW, j * bH, bW, bH);
		}
	}

	fW = 95;
	fH = 95;
	x = WINDOW_WIDTH / 2 - fW / 2;
	y = WINDOW_WIDTH / 2 - fH / 2;

	for (i = 0; i < CLIPS_AMOUNT; i++) {
		clips[i].x = i / 2 * fW;
		clips[i].y = i % 2 * fH;

		clips[i].w = fW;
		clips[i].h = fH;
	}

	SDL_QueryTexture(fg, NULL, NULL, &sW, &sH);
	renderTexture(fg, ren, x, y);

	SDL_RenderPresent(ren);

	SDL_Delay(2000);

	while (!quit) {
		while (SDL_PollEvent(&e)) {
			if (e.type == SDL_QUIT) quit = 1;
			//if (e.type == SDL_MOUSEBUTTONDOWN) quit = 1;
		}

		if (kbState[SDL_SCANCODE_W] || kbState[SDL_SCANCODE_UP]) {
			x -= 5; curClip = 3;
			if (x < -sW) x = WINDOW_WIDTH - sW;
		}
		
		if (kbState[SDL_SCANCODE_A] || kbState[SDL_SCANCODE_LEFT]) {
			y -= 5; curClip = 0;
			if (y < -sH) y = WINDOW_HEIGHT + sH;
		}
		
		if (kbState[SDL_SCANCODE_S] || kbState[SDL_SCANCODE_DOWN]) {
			x += 5; curClip = 1;
			if (x > WINDOW_WIDTH - sW) x = 0 -sW;
		}
		
		if (kbState[SDL_SCANCODE_D] || kbState[SDL_SCANCODE_RIGHT]) {
			y += 5; curClip = 2;
			if (y > WINDOW_HEIGHT + sH) y = 0 -sH;
		}

		SDL_RenderClear(ren);

		for (i = 0; i < WINDOW_WIDTH / bW; i++) {
			for (j = 0; j <= WINDOW_HEIGHT / bH; j++) {
				renderTextureS(bg, ren, i * bW, j * bH, bW, bH);
			}
		}

		renderSprite(fg, ren, x, y, &clips[curClip]);
		SDL_RenderPresent(ren);
	}

	SDL_DestroyTexture(bg);
	SDL_DestroyTexture(fg);
	SDL_DestroyRenderer(ren);
	SDL_DestroyWindow(win);

	cleanUp();

	return 0;
}
Beispiel #30
0
void GUIManager::renderTexture(GLuint TextureId, GLuint FboTarget, Rect rect)
{
    renderTexture(TextureId, FboTarget, rect.x, rect.y, rect.w, rect.h);
}