Hi I get this error
error: incompatible types in assignment of `int' to `unsigned char*[((unsigned int)((int)imageSize))]'
with this code......
if (image.HasAlpha())
{
unsigned long imageSize = image.GetWidth() * image.GetHeight() * 4;
unsigned char* imageData = image.GetData();
unsigned char* imageAlpha = image.GetAlpha();
unsigned char* data[imageSize];
for (unsigned long index = 0; index < imageSize; index += 4)
{
memcpy(data, imageData, 3);
imageData += 3;
memcpy(data + 3, imageAlpha, 1);
imageAlpha++;
data += 4;
}
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, image.GetWidth(), image.GetHeight(),
GL_RGB, GL_UNSIGNED_BYTE, data);
}
Help me please...... Sorry for the beginner question? I just don't get it I used to do stuff like increaseing the ptr with += all the time.....
EDIT:
Let me say that it fails on the line
:P
Hi,
this should work
if (image.HasAlpha())
{
unsigned long imageSize = (image.GetWidth() * image.GetHeight()) << 2;
unsigned char* imageData = image.GetData();
unsigned char* imageAlpha = image.GetAlpha();
unsigned char* data[imageSize];
for (unsigned long index = 0; index < imageSize; index += 4)
{
memcpy(data, imageData, 3);
imageData += 3;
memcpy(data + 3, imageAlpha, 1);
++imageAlpha;
(*data) += 4;
}
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, image.GetWidth(), image.GetHeight(),
GL_RGB, GL_UNSIGNED_BYTE, data);
}
// Edit: Ah, you're faster :P
is the same as
@polygon7
Why the instead of ?
This line
unsigned long imageSize = (image.GetWidth() * image.GetHeight()) << 2;
@Michael How to use a C++ style cast in this case??? Yes I know static_cast, dynamic_cast etc. How to use it here???
@Michael yeah I always use C++ casts instead of C casts. I couldn't figure out how to get it to work here. What type do I need to static_cast to?? I tried static_cast, but I couldn't figure out which type to cast to. :P
For example, you can use it to cast a double to an int:
double myDouble = 3.0;
int myInt = static_cast<int>(myDouble);
Anyway, be careful because static casts could be dangerous.
Have a look at the links I have suggested. There is a lot of examples and useful bla bla :).
Michael
I tried
static_cast<unsigned char* []>(data) += 4;
and it didn't work.
error: invalid static_cast from type `unsigned char*[((unsigned int)((int)imageSize))]' to type `unsigned char*[]'
What type (int, char, etc.) do I need to cast to???
Nevermind. This works, and better too I think. :)
if (image.HasAlpha())
{
unsigned long imageSize = image.GetWidth() * image.GetHeight() * 4;
unsigned char* imageData = image.GetData();
unsigned char* imageAlpha = image.GetAlpha();
unsigned char* data = new unsigned char[imageSize];
for (unsigned long index = 0; index < imageSize; index += 4)
{
memcpy(data, imageData, 3);
imageData += 3;
memcpy(data + 3, imageAlpha, 1);
++imageAlpha;
data += 4;
}
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, image.GetWidth(), image.GetHeight(),
GL_RGB, GL_UNSIGNED_BYTE, data);
delete[] data;
}
How do I use try/catch?
unsigned char* data;
// Try to allocate some memory.
try
{
data = new unsigned char[imageSize];
}
// Show an exception message if we can't allocate the memory we need.
catch(bad_allocation)
{
wxSafeShowMessage(_("Exception"), _("Memory allocation failed! The application will terminate immediately..."));
}
Sorry I'm new to this type of error handling.... :P
Thomas makes a good point but I don't you think you should throw them away out of hand. Almost everyone spends time making "optimizations" on areas that don't need any. I say use exceptions carefully and if you run into performance problems pull back there use.
Take a look at Ogre (http://www.ogre3d.org). It is a high performance 3D rendering engine. It uses exceptions, but only where they are appropriate, like when a resource fails to run or you have done something to crash the render system.
The overall important rule for exceptions to use them for program flow control, that means don't check for thing with exceptions. This rough java snippet below is a good example of what not to do:
int[] myArray = new int[10];
try {
int index = 0;
while(true) {
system.out.println(myArray[index])
index++;
}
} catch (ArrayIndexOutOfBoundsException e)
// Finished the loop
}
There exist a no_throw new. I think it is not portable, the gurus (Sutter, ..) give the advice not to use it. So just forget you ever heard about it ;-)
I have had a look at the C++ Standard book and I have found that the two possibilities for the operator new are described and there are no constraints in using the throw-new instead of the nothrow-new. There is written something as: "If the program wishes...". Personally, I have never used the nothrow-new, but I prefer it, even if I have to specify nothrow, e.g.:
int* ndata = new(nothrow) int;
//do something with ndata
delete ndata;
I have also not found portability issues by searching with Google. May be this is due on how C++ standard compliant are the different compilers.
Michael
Well, if you take a look at the compiler's output, even without optimizations (GCC), using * 4 or << 2 generate the same code, but * 4 is still more readable.
For this code:
The compiler outputs:
movl -4(%ebp), %eax
sall $2, %eax
movl %eax, -8(%ebp)
There're many things compilers can handle by themselves, including such simple optimizations, and a few more advanced ones that can only be achieved in ASM.
Specifying optimizations (like -O3) will give you better results, like converting:
into:
movl -4(%ebp), %edx
leal (%edx,%edx,4), %eax
movl %eax, 4(%esp)
As you can see those early optimizations just make your code less readable for the same output and performance.