Trouble reading bitmap files 2

The Partridge Family were neither partridges nor a family. Discuss.
Paul
Posts: 18
Joined: December 31st, 2021, 1:45 am

Trouble reading bitmap files 2

Post by Paul » January 3rd, 2022, 8:01 pm

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. :(
Attachments
Updated Paul's Space Empire Strategy Game.zip
(368.14 KiB) Downloaded 61 times

albinopapa
Posts: 4370
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Trouble reading bitmap files 2

Post by albinopapa » January 3rd, 2022, 9:47 pm

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.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4370
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Trouble reading bitmap files 2

Post by albinopapa » January 3rd, 2022, 9:50 pm

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.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Paul
Posts: 18
Joined: December 31st, 2021, 1:45 am

Re: Trouble reading bitmap files 2

Post by Paul » January 3rd, 2022, 11:03 pm

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 :) .

albinopapa
Posts: 4370
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Trouble reading bitmap files 2

Post by albinopapa » January 3rd, 2022, 11:35 pm

sweet
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Paul
Posts: 18
Joined: December 31st, 2021, 1:45 am

Re: Trouble reading bitmap files 2

Post by Paul » January 4th, 2022, 7:41 am

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.

Paul
Posts: 18
Joined: December 31st, 2021, 1:45 am

Re: Trouble reading bitmap files 2

Post by Paul » January 4th, 2022, 7:43 am

oops forgot to attach the updated code.
Attachments
ReUpdated Paul's Space Empire Strategy Game.zip
(388.55 KiB) Downloaded 62 times

albinopapa
Posts: 4370
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Trouble reading bitmap files 2

Post by albinopapa » January 4th, 2022, 8:45 am

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.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4370
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Trouble reading bitmap files 2

Post by albinopapa » January 4th, 2022, 9:02 am

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.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Paul
Posts: 18
Joined: December 31st, 2021, 1:45 am

Re: Trouble reading bitmap files 2

Post by Paul » January 4th, 2022, 6:16 pm

OMG it works!!! I put (ch <= '~') and it prints! I'll consider your other changes as well. Thanks so much!

Post Reply