Jump to content

  • Log In with Google      Sign In   
  • Create Account

doyleman77

Member Since 14 Apr 2010
Offline Last Active Oct 30 2014 09:20 AM

Topics I've Started

SDL_BlendMode when sharing textures?

19 July 2014 - 10:23 AM

I'm using a map to store all of my textures, from which entity's refer upon. There may be an instance where I want one entity whom shares a texture with possibly many other entities to be transparent or blended, but not the others.

 

Right off the bat, I thought about having flag or check per entity on their alpha or blend mode, and modifying the texture just before drawing, and then resetting the texture's blend & alpha (this way, all entities will eventually be able to modify the texture as they need, and it'd default when done).

 

I am not sure if that'd work, though I *think* so. My question, though, is: is there a more optimal way of handling blending and alpha on a texture that is shared?


STL Map and SDL2 Texture segfault

06 July 2014 - 06:35 AM

I've been to figure out why this keeps breaking for the last 3 days. I'm using a std::map to store all textures in, hopefully, and then just have my link to the images from that map. This works fine on one image; I can load it, store it, and retrieve it to have an entity use it and finally, it gets drawn properly. The minute I add in another texture, however, my game crashes on trying to create an entity that refers to any/either of the textures - GDB has it crashing on a segfault inside the std::map header file, presumably when I'm trying to retrieve the texture.

 

Here's the code in question:

 

Game.cpp

Game::Game()
{
  time(&gameTime);
  timeInfo = localtime(&gameTime);

  gameWindow = SDL_CreateWindow(globals::SCREENTITLE.c_str(),
              SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
              globals::SCREENWIDTH, globals::SCREENHEIGHT, SDL_WINDOW_SHOWN);

  gameRenderer = SDL_CreateRenderer(gameWindow, 0, SDL_RENDERER_ACCELERATED);

  loadTexture("texture.png");
  loadTexture("raindrop.png");
  //gameVector.push_back(&(textureLibrary["raindrop.png"]));

  Entity newEntity(textureLibrary["raindrop.png"]);
  //Entity anotherEntity(&(textureLibrary["raindrop.png"]));
  gameVec.push_back(&(newEntity));
  //gameVec.push_back(&(anotherEntity));

  running = true;
  while(running)
  {
    handleInput(gameInput);
    update();
    draw();
    SDL_Delay(16);
  }
};

Game.cpp

void Game::loadTexture(std::string filename)
{
  SDL_Texture* newTexture = NULL;

  SDL_Surface* loadedSurface = IMG_Load(filename.c_str());
  if(loadedSurface == NULL)
    SDL_Quit();
  /// Set the image invisibility color
  SDL_SetColorKey(loadedSurface, SDL_TRUE, SDL_MapRGB(loadedSurface->format, 0xFF, 0, 0xFF));

  newTexture = SDL_CreateTextureFromSurface(gameRenderer, loadedSurface);

  SDL_FreeSurface(loadedSurface);
  textureLibrary[filename.c_str()] = newTexture;
  return;
};

And finally, my entity constructor - very basic.

Entity::Entity(SDL_Texture* itsTexture)
{
    texture = itsTexture;
    SDL_QueryTexture(itsTexture, NULL, NULL, &texRect->w, &texRect->h);
};

texture is the member of Entity, and is a simple SDL_Texture*.

 

Again; this system worked fine when I had only one image loaded - it wasnt until I had both ["raindrop.png"] and ["texture.png"] that it started segfaulting on the

Entity newEntity(textureLibrary["raindrop.png"]);

bit.

 

Any clues? Don't mind the terrible coding. This is me trying to get a game worked out in a 8wk challenge; and I'm sure it may be sloppy. :(


I don't get c++11.

18 February 2014 - 09:43 PM

Pretty comfortable with my c++ code now; I get classes, I get using templates and STL, smart pointers, and to an extent, exceptions. What I don't get is most of the new 2011 standard. Lambdas confuse me, in both their syntax and their purpose.

 

I've looked up articles trying to show practical uses for them, but they seem to go even further above my head than just the word 'lambda'. Rvalue / moving seems confusing just as well; decltype... gah. None of the new stuff makes much of any sense. The only thing I've understood so far is auto, and to a half-way extent: the new for loop style.

 

I've read through a third of C++ Primer 5th, and so far it's not really shed any new light to me. The frustration of these new features and my inability to grasp them has me concerned... How is it some clicked onto the new concepts rather fast? And where can I go to get to that point?


SDL_CreateTextureFromSurface issue

26 January 2014 - 08:46 PM

I noticed that CreateTextureFromSurface wants a renderer and a surface passed in as arguments.

 

I was trying to seperate sprites from the game window, which holds the renderer; and am not quite sure how to do it.

 

At the moment, I have a Window class which holds the window and renderer, and a Sprite, which has image loading capabilities. An image should be able to load it's own image without knowing about the window; or that's the idea I was chasing after.

 

Do I really have to pass a Window down by reference into Sprite, so that Sprite can get() a renderer from the Window?


C++ Merge sort is duplicating data

23 September 2013 - 05:02 PM

I've been trying to get a merge sort to fix character arrays for a week now, and I cannot figure out for the life of me why I am getting half way sorted and then it just doubles everything. Here's what I have so far:

 

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <stdlib.h>
#include <dos.h>
#include <time.h>
#include <sys/timeb.h>

using namespace std;

void MyMergeSort(int sourceArray[], int destinationArray[], int low, int high)
{
	if (low != high)
	{
		int middle = (low+high)/2;
		MyMergeSort(destinationArray, sourceArray, low, middle);
		MyMergeSort(destinationArray, sourceArray, middle+1, high);
		cout << "Merged: " << low << ", " << middle << " with " << middle+1 << ", " << high << endl;
		int start1 = low;
		int end1 = middle;
		int start2 = middle+1;
		int end2 = high;
		int destinationCount = 0;
		while ((start1 <= end1) && (start2 <= end2))
		{
			if(sourceArray[start1] < sourceArray[start2])
			{
				destinationArray[destinationCount] = sourceArray[start1];
				start1++;
			}
			else
			{
				destinationArray[destinationCount] = sourceArray[start2];
				start2++;
			}
			destinationCount++;
		}
		for(; start1 <= end1; start1++, destinationCount++)
		{
				destinationArray[destinationCount] = sourceArray[start1];
				cout << "in for loop start1" << endl;
		}
		for(; start2 <= end2; start2++, destinationCount++)
		{
			destinationArray[destinationCount] = sourceArray[start2];
			cout << "in for loop start2" << endl;
		}
	}
}

void main()
{
	int samplearray[10] ={10, 20, 30, 40, 50, 5, 6, 7, 35, 38};
	int copyarray[10];
	for (int x = 0; x < 10; x++)
	{
		copyarray[x] = samplearray[x];
	}
	
	MyMergeSort(copyarray, samplearray, 0, 9);
	cout << "Sample Array Contents:" << endl;
	for (int x = 0; x < 10; x++)
	{
		cout << samplearray[x] << " ";
	};
	cout << endl << endl;
	cout << "Copy Array Contents:" << endl;
	for (int x = 0; x < 10; x++)
	{
		cout << copyarray[x] << " ";
	};
	
   for (int z=start;z<=finish;z++)
   {
	   cout << MyArray[z] << "  ";
   }
   */
	int hold;
	cin >> hold;
}

PARTNERS