Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1] 2

Author Topic: C and .bmp files....  (Read 3178 times)

qwertyuiopas

  • Bay Watcher
  • Photoshop is for elves who cannot use MSPaint.
    • View Profile
    • uristqwerty.ca, my current (barren) site.
C and .bmp files....
« on: June 19, 2008, 10:26:52 am »

I am interested in C, and recently wanted to make an opengl kind of game.
I am aiming in the direction of something based of of Dwarf Fortress, and have come up with the problem of displaying stuff and gotten over it, but now I want to load data from a .bmp file(one of the font files for DF ;) but don't know how.

Can anyone help me?
Also, I am loading it into arrays of bits for glBitmap(); so I need to get and compare the colors of each pixel.


Bonus points for whoever know what I am planning to do with such knowledge.
Logged
Eh?
Eh!

nerdpride

  • Bay Watcher
    • View Profile
Re: C and .bmp files....
« Reply #1 on: June 19, 2008, 01:35:58 pm »

I'm just starting to move away from Curses into SDL, so no help from me.

But I'm also interested in hearing about this.
Logged

Asheron

  • Bay Watcher
  • Look in to my eyesssss.
    • View Profile
    • http://www.ihavenoideathissiteexcisted.com
Re: C and .bmp files....
« Reply #2 on: June 19, 2008, 01:44:36 pm »

so am I. I am only dabbling in Python, so I won't be able to help you.
I might recommend that you begin draw out the game before you actually start programming. Set goals and that sort of motivational stuff.
Or else you will get bored.
VERY bored.
Logged


Quote from: Toady One
Did you just post a bunch of vegi-dicks on my board?  I've been trying to combat forum devolution a bit, and that involves fewer vegi-dicks!
Quote from: Yahtzee
Yes, random is funny, isn't it? Sometimes I set up a random number generator when I need a good laugh.

Keiseth

  • Bay Watcher
  • Better known as The Mimic.
    • View Profile
Re: C and .bmp files....
« Reply #3 on: June 19, 2008, 01:59:47 pm »

I'm just starting to move away from Curses into SDL, so no help from me.

But I'm also interested in hearing about this.

I just moved away from SDL into Curses, so I'm of no help either but I'm also interested. Gamedev seems to have a page: http://www.gamedev.net/reference/articles/article1966.asp

Edit: Whoops, that's C++. Maybe it can still be of use to you.

Looks like one heck of an exercise.
Logged
Cronocke: Today's lesson, revised: Don't listen to Mimic. He will kill you, and it will be entirely by accident. Repeatedly. While he's on fire.

qwertyuiopas

  • Bay Watcher
  • Photoshop is for elves who cannot use MSPaint.
    • View Profile
    • uristqwerty.ca, my current (barren) site.
Re: C and .bmp files....
« Reply #4 on: June 19, 2008, 02:49:43 pm »

Well, the opengl thing I found takes a tile in the form of a bitmap or something where a byte says if there is a pixel on each space and if there is, it fills it with the current color, so it would work perfectly for Dwarf Fortress style graphics with a background polygon to cover what was there and be the background.

The hard part is reading the data.

Anyways, I was distracted by DF adventure mode, and now I have to cut the grass, but I will get this done *eventually*
Logged
Eh?
Eh!

qwertyuiopas

  • Bay Watcher
  • Photoshop is for elves who cannot use MSPaint.
    • View Profile
    • uristqwerty.ca, my current (barren) site.
Re: C and .bmp files....
« Reply #5 on: June 19, 2008, 09:11:25 pm »

Well, I have 5 characters worth of the DF font scrolling 1 pixel per frame downward, looping, and the color based on the mouse location.

If you want it ot the code, mention it and I may get back to you.

Next comes a grid to display(80x40 tile window) and after that, well, gameplay.
Logged
Eh?
Eh!

Keiseth

  • Bay Watcher
  • Better known as The Mimic.
    • View Profile
Re: C and .bmp files....
« Reply #6 on: June 20, 2008, 12:19:29 am »

I'd love a copy or snippet of the code to study. It'd be nice not to be explicitly chained to say, SDL, even if it's under a permissive license. File reading/writing is my weakness, too... drove me nuts trying to figure out what ifsteam/ofstream/fstream was returning, in C++.

By the way, you're writing a game in C? That sounds kinda tricky, down the line. LCS for example, tons of switch statements. You're a braver man than I, for I would be naught without my classes and objects!
Logged
Cronocke: Today's lesson, revised: Don't listen to Mimic. He will kill you, and it will be entirely by accident. Repeatedly. While he's on fire.

Fualkner

  • Bay Watcher
  • My glasses split light.
    • View Profile
Re: C and .bmp files....
« Reply #7 on: June 20, 2008, 12:40:52 am »

I would love to learn to code. I know the basics of DM and I rock at TIBasic (But then again, so does a 4 year old.)

Anyone point me to an appropriate resource for learning?
Logged

Keiseth

  • Bay Watcher
  • Better known as The Mimic.
    • View Profile
Re: C and .bmp files....
« Reply #8 on: June 20, 2008, 01:15:31 am »

Maybe I can offer a few links until somebody who knows better ones comes around!

Cplusplus' C++ tutorial is pretty good, but probably not the best starting point. First thing I thought of, though. http://www.cplusplus.com/doc/tutorial/ - C++ is a hard language to start with, though, and setting up a compiler and all that can be a real headache without a guide next to you. Still, you can do just about anything in it.

If you want to make a game to start with, I'd recommend the Sphere engine (http://www.spheredev.org/wiki/Main_Page) -- It comes with everything you need, a development environment with an image editor and so on. It uses Spidermonkey Javascript, which is very similar in syntax to C/C++. What makes it a good choice is Sphere's generally helpful error messages and the very permissive nature of SM Javascript. The forum is also very friendly. It isn't suitable for making 3D games, however, it can handle anything 2D very well. It's Free-Open-Source-Software.

Sphere might abstract the nature of programming too much, however, it might be worth looking into, and I enjoyed my time with it immensely. I started knowing basically nothing at all and it taught me a lot. Edit: Also, the similar syntax with C/C++ would allow you to learn those a bit easier afterwards, I think. That's what I did, anyway.

Python http://www.python.org/ is also a very user-friendly and very useful scripting language. I don't know the language myself but I've heard nothing but good about it. Dwarf Companion was programmed using it, and it's very adaptable. I'm pretty positive it'd be easier to start with than C++, too, and I am positive that it can be set up more quickly.
« Last Edit: June 20, 2008, 01:18:54 am by Keiseth »
Logged
Cronocke: Today's lesson, revised: Don't listen to Mimic. He will kill you, and it will be entirely by accident. Repeatedly. While he's on fire.

DJ

  • Bay Watcher
    • View Profile
Re: C and .bmp files....
« Reply #9 on: June 20, 2008, 03:16:53 am »

Are you dead-set on OpenGL? If not, I suggest switching to SDL. Graphics in SDL are a piece of cake. You get SDL_LoadBMP(), SDL_SetColorKey(), SDL_BlitSurface() and SDL_Flip(), and that's pretty much everything you need.

I also suggest looking into C++. I was making a URW clone in C, but somewhere around 2000 lines my code became too spaghetti-like to implement new features in a timely fashion. For every hour I spent adding features I needed to spend three removing obscure bugs these new features introduced, so I gave up. Encapsulation would've made my life a lot easier, so now I'm working on a less ambitious project (a generic sidescroller) to learn C++ before I revisit the first project.
Logged
Urist, President has immigrated to your fortress!
Urist, President mandates the Dwarven Bill of Rights.

Cue magma.
Ah, the Magma Carta...

qwertyuiopas

  • Bay Watcher
  • Photoshop is for elves who cannot use MSPaint.
    • View Profile
    • uristqwerty.ca, my current (barren) site.
Re: C and .bmp files....
« Reply #10 on: June 20, 2008, 07:22:52 am »

SDL uses opengl, it just does the work for you to make it simpler!

Here is what I have for loading the bmp, I copied some of it from some thing from a google search, then wrote a custom part for loading tha data.

Code: [Select]
    FILE *bmp=fopen("curses_640x300.bmp","r");
    if(!bmp)
    {
        printf("bad filename.\n");
        system("pause");
        return 1;
    }
    BITMAPFILEHEADER header;
    if(fread(&header,sizeof(BITMAPFILEHEADER),1,bmp)<1)
    {
        printf("Error reading.\n");
        fclose(bmp);
        system("pause");
        return 2;
    }
    int p=sizeof(BITMAPFILEHEADER);
    while(p<header.bfOffBits){fgetc(bmp);p++;}
    int y=0;
    int x,r,c;
    while(y<16)
    {
        r=0;
        while(r<12)
        {
            x=0;
            while(x<16)
            {
                c=0;
                while(c<8)
                {
                    fgetc(bmp);
                    myfont[(y*16+x)*12+r]=myfont[(y*16+x)*12+r]*2+(fgetc(bmp)!=0);
                    fgetc(bmp);
                    c++;
                }
                x++;
            }
            r++;
        }
        y++;
    }
    fclose(bmp);



Also how it's used:
Code: [Select]
GLubyte myfont[256*12];
int o;
void render()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glLoadIdentity();
    glColor3f(mousex/640.0,0.5,mousey/480.0);
    glRasterPos2i(0.1,0.1);
    glBitmap(8,12,0,0,0,12,myfont+o);
    glBitmap(8,12,0,0,0,12,myfont+o+12);
    glBitmap(8,12,0,0,0,12,myfont+o+24);
    glBitmap(8,12,0,0,0,12,myfont+o+36);
    glBitmap(8,12,0,0,0,12,myfont+o+48);
    glBitmap(8,12,0,0,0,12,myfont+o+60);
    SwapBuffers(hDC);
}
o is incremented each frame until it would show the last tile where it is set back to 0.
Logged
Eh?
Eh!

qwertyuiopas

  • Bay Watcher
  • Photoshop is for elves who cannot use MSPaint.
    • View Profile
    • uristqwerty.ca, my current (barren) site.
Re: C and .bmp files....
« Reply #11 on: June 20, 2008, 12:49:45 pm »

Heres a picture of what I finally have working. Except the text. I don't know what went wrong there. And it's stored backwards.




edit: okay, it is just the y coordinate that is reversed while loading.


edit again: I fixed that, but there is some overlap in the font array so characters around 144 or so seem to be refrencing other perts of the memory( although it IS interesting that numlock shows me the ms/frame display judging by a flickering pixel...


and again: it would appear that the "first" part of the bitmap it loads is forgotten and used for other memory. so the bottom half of the bitmap(loaded firts) is being cut off....
« Last Edit: June 20, 2008, 01:55:28 pm by qwertyuiopas »
Logged
Eh?
Eh!

Fualkner

  • Bay Watcher
  • My glasses split light.
    • View Profile
Re: C and .bmp files....
« Reply #12 on: June 20, 2008, 02:56:10 pm »

Sooooo... What is it?
Logged

nerdpride

  • Bay Watcher
    • View Profile
Re: C and .bmp files....
« Reply #13 on: June 20, 2008, 03:11:27 pm »

I'll try to understand.

Code: [Select]
                while(c<8)
                {
                    fgetc(bmp);
                    myfont[(y*16+x)*12+r]=myfont[(y*16+x)*12+r]*2+(fgetc(bmp)!=0);
                    fgetc(bmp);
                    c++;
                }

I don't get why you need to use fgetc twice (three times?).


The line in between them just looks silly.  Maybe it can be simplified to:
Code: [Select]
myfont[(y*16+x)*12+r] *= 2 + (fgetc(bmp)!=0);But this is be tricky.  Do you want
Code: [Select]
myfont[foo] *= 2;
myfont[foo] += (fgetc(bmp)!=0);
or...
Code: [Select]
myfont[foo] *= (2 + (fgetc(bmp)!=0));because I'm pretty sure they're different but less sure which will default to.

Just a little nitpick, wouldn't this be less confusing if you used FOR loops instead of WHILE?
Code: [Select]
for (y = 0; y < 16; y++)
{
etc();
}
you know.

Quote
mousex
Tee hee hee  :D
Logged

qwertyuiopas

  • Bay Watcher
  • Photoshop is for elves who cannot use MSPaint.
    • View Profile
    • uristqwerty.ca, my current (barren) site.
Re: C and .bmp files....
« Reply #14 on: June 20, 2008, 03:23:11 pm »

a) for loops,(in my experience) will not always be supported without some compiler option, the spare fgetc()s read and discard th ered and blue values.


Anyways, the problem I am having is that the first half of the font array is being ignored for some reason.
Logged
Eh?
Eh!
Pages: [1] 2