3
\$\begingroup\$

So ive always had problems with buffers and never used them before. I read up on the topic online but since winbgim is so old i couldnt find any definite answers to the topic. So im gonna ask them here. I found getimage and putimage but i dont know if those are what im looking for. Maybe theres a much better option!

What i have done so far:

  • Ive searched for the answer online.
  • I looked through some posts here on the forum
  • I looked at the winbgim docs

My current code:

My current code is using the readimagefile function and it does work well. But the problem is that on systems that have an os greater than win7 ( i dont know why its fine on win 7 ) the disk is used 100% because it keeps reading the images over and over again from the actual files rather thier buffers.

Heres the decoder function i made. Its pretty messy. I just made it. Im still working on it. Its reading from a globally defined array ( I dont wanna use an dynamic array for personal reasons so i had to put it there because a [400][13] array kinda produces an stack overflow error )

Here it is:

 void decodelevelAndDraw(int minx, int maxx) {
    int x = 0, y = 0;
    while (y != 13) {
        while (x != maxx) {
            if (levelcode[x][y] == 1) {
                //x -= minx;
                readimagefile("question.jpg", x * 70 - minx * 70, y * 70, 70 + x * 70 - minx * 70, 70 + y * 70);
                //printf("Block added at %d;%d", x * 70, y * 70);
            }
            else if (levelcode[x][y] == 2) {

                if (levelcode[x][y - 1] == 2) {
                    readimagefile("DRECK.jpg", x * 70 - minx * 70, y * 70, 70 + x * 70 - minx * 70, 70 + y * 70);

                }
                else if (levelcode[x + 1][y]== 2 && levelcode[x - 1][y] != 2) {
                    readimagefile("grass_side.jpg", x * 70 - minx * 70, y * 70, 70 + x * 70 - minx * 70, 70 + y * 70);

                }
                else if (levelcode[x - 1][y] != 2 && levelcode[x][y - 1] == 2) {
                    readimagefile("dreck_side.jpg", x * 70 - minx * 70, y * 70, 70 + x * 70 - minx * 70, 70 + y * 70);

                }
                else if (levelcode[x + 1][y] != 2 && levelcode[x][y - 1] == 2) {
                    readimagefile("dreck_side_right.jpg", x * 70 - minx * 70, y * 70, 70 + x * 70 - minx * 70, 70 + y * 70);

                }
                else if (levelcode[x - 1][y] == 2 && levelcode[x + 1][y] != 2) {
                    readimagefile("grass_side_right.jpg", x * 70 - minx * 70, y * 70, 70 + x * 70 - minx * 70, 70 + y * 70);

                }

                else readimagefile("grass.jpg", x * 70 - minx * 70, y * 70, 70 + x * 70 - minx * 70, 70 + y * 70);

            }
            else if (levelcode[x][y] == 3) {
                readimagefile("1Punch.jpg", x * 70 - minx * 70, y * 70, 70 + x * 70 - minx * 70, 70 + y * 70);

            }
            x++;
        }
        x = 0;
        y++;
    }
}

More information:

So this is for a class project so i have to use the winbgim version of graphics.h . Im using visual studio 2015 community edition with a version identical to this one for the mingw compiler. If you need like the full project feel free to ask. I will send you anything you need.

So my question is: How can i load an image into a buffer so it can be loaded quicker into the game without slowing down the dd? Please add precise and full explanations so I understand the code instead of just copy and pasting it from your answer / some websites ! I am at a loss here and could really use some of your help.

Have a nice afternoon!

\$\endgroup\$
1
\$\begingroup\$

So, as you know, readimagefile displays an image to the screen by reading it off the disk, and on some systems, it apparently only ever reads it from the disk without caching the image. So, what we need to do is move calls to readimagefile out of the loop and into some initializing portion of the code. You mentioned getimage and putimage in your question, and I think that may be the key to solving your problem, because if you use readimagefile to put the image on the screen from the disk, and then grab the image from the screen and store it in memory using getimage, you can call putimage many times in a loop without worrying about loading the image from the disk every frame. So, I haven't actually run this code, so I can't guarantee that it works because I'm unfamiliar with the library, it involves void pointers and somewhat C-like memory management, and I'm assuming you want C++. That being said, here is what I think you could do.

const unsigned int IMAGE_WIDTH = 32, IMAGE_HEIGHT = 32;
//Allocate the memory we need so we can store the image. Allocated as char here, as they are essentially always a single byte,
// and I'm assuming the imagesize function wants the count for the space needed to be in bytes.
void* test_image = new char[imagesize(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT)];
//Put the image on-screen from disk
readimagefile("yourimage.jpg", 0, 0, IMAGE_WIDTH, IMAGE_HEIGHT);
//Take the image from the screen and put it in memory
getimage(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT, test_image);
//Maybe put something here to clear the screen so you can perform the same process again for another image

while (some_condition) { //Just a generic indication that you have a loop
    /*some necessary code before*/

    //Feel free to call putimage in the loop
    putimage(43, 75, test_image, COPY_PUT);

    /*some necessary code after*/
}

//If you're not going to use the image anymore, free up its memory
delete[] test_image;
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.