Page 1 of 2

Trouble reading bitmap files 2

Posted: January 3rd, 2022, 8:01 pm
by Paul
Hi, I'm trying to make a space strategy game starting with the snek starter. I ran into some real problems when I tried to implement a Surface class to read from a bitmap file. I've tried reading from multiple bitmap file font sheets(should all be included in my project as resource files) and it seems to be reading them wrong because the bmInfoHeader.biBitCount keeps coming up as 52428 which can't be right. This is causing my program to not work. I don't know why this is happening because I copied the Surface constructor pretty much exactly from chili's intermediate c++ tutorial 10. :(

Re: Trouble reading bitmap files 2

Posted: January 3rd, 2022, 9:47 pm
by albinopapa
There are several things to address here, but I'll address the bitmap info thing first.

Something to add to the loading of the file in Surface that might shed some light on the subject.

Code: Select all

std::ifstream file( filename, std::ios::binary );
if( !file.is_open() ) 
    throw std::runtime_error( "File not in current directory." );
You'll have to #include <stdexcept> to get that to work.

You could use assert( file.is_open() ), but that only works when in Debug mode so if a file goes missing after a Release build, the only way to know is a later crash somewhere else.

One thing I've seen others do is have a default sprite, just a magenta square will do. If the file is missing that you want to load, you use the default. This keeps the program from crashing, and you get to see visually which textures might be missing.

I had to find a fixedsys_16x28.bmp file from another project in order to get it to work...along with the many other fixes that I'll describe if you find them later on.

Re: Trouble reading bitmap files 2

Posted: January 3rd, 2022, 9:50 pm
by albinopapa
The other thing I would do is initialize your bitmap header and bitmap info header using = {}; This sets all members to 0. Without initializing to 0, you get random numbers like the 52428 number for instance. This would be another indicator that something didn't load. This also makes it way simpler to test for...

Code: Select all

if( some_var == 0 )
   // handle error
With random values, you don't know what to test for other than is not correct value.

Re: Trouble reading bitmap files 2

Posted: January 3rd, 2022, 11:03 pm
by Paul
Thanks for the response! :D I've made the changes you suggested and added the bmp file to engine and that solves that error. Now ill work on the next set of errors :) .

Re: Trouble reading bitmap files 2

Posted: January 3rd, 2022, 11:35 pm
by albinopapa
sweet

Re: Trouble reading bitmap files 2

Posted: January 4th, 2022, 7:41 am
by Paul
I've fixed all the compiler errors but now it just doesn't print the text. :lol: . I'm stuck again. Any help or hints would be much appreciated.

Re: Trouble reading bitmap files 2

Posted: January 4th, 2022, 7:43 am
by Paul
oops forgot to attach the updated code.

Re: Trouble reading bitmap files 2

Posted: January 4th, 2022, 8:45 am
by albinopapa
Look at this line in Font::drawText

Code: Select all

		if (ch >= '~')
You sure ch should be >= '~'?
Printable characters are between space ' ' and tilde '~' or 30 - 126 inclusive.

Re: Trouble reading bitmap files 2

Posted: January 4th, 2022, 9:02 am
by albinopapa
Looks like you fixed most of the things I had posted about previously.

The only thing I still see is:

Code: Select all

	if (count < numplayers)
		Game(*wind);
The compiler will probably skip over this because you aren't assigning Game to anything.

Right now, the only other advice I might have is not using new/delete, but instead use std::unique_ptr or std::vector depending on your needs. Also, maybe decide if allocating an object on the heap is even necessary. In Game::Game you have:

Code: Select all

	Planet* hp = new Planet[GALAXY_SIZE];
That could easily be an

Code: Select all

std::vector<Planet> hp( GALAXY_SIZE );
The best reason for using these utility classes is quality of life, but they're also safer than just using new/delete. At some point, you may end up forgetting to delete a pointer or maybe forget you already deleted a pointer and try reusing it. It's just good practice to remove some of the thought power in memory management and leave it up to the compiler by using the C++ utility classes.

Re: Trouble reading bitmap files 2

Posted: January 4th, 2022, 6:16 pm
by Paul
OMG it works!!! I put (ch <= '~') and it prints! I'll consider your other changes as well. Thanks so much!