Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: [C++] Help!  (Read 1892 times)

0 Members and 1 Guest are viewing this topic.

PoeFacedKilla

Prussian Killer Bee
[C++] Help!
« on: November 20, 2009, 02:02:29 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 269
Ok, I have been reading a c++ book lately, and I have definitely got the hang of it.  But the only problem I am having is with making a program more than one file.  So can somebody help me?

main.cpp
Code: [Select]
#include <allegro.h>
#include "header.h"

int main( int argc, char *argv[] )
{
 
    allegro_init();
    install_keyboard();
   
    set_window_title( "SPACEWAR" );
    set_color_depth( 24 );
    set_gfx_mode( GFX_AUTODETECT, 680, 420, 0, 0 );
   
    if( ! ( KEY_ESC ) )
    {
   
        draw_sprite( ship1, 0, 0 );

       
    }
   
    readkey();
   
    return 0;
   
}

END_OF_MAIN()

header.h
Code: [Select]
    BITMAP *ship1 = NULL
    ship1 = load_bitmap( "ship1.png", NULL );
Logged
the Indyboard - User Generated Social Forum | Now With Even More Discussion!
Poe, The Independent Programmer
  • Zelda Shrine
Re: [C++] Help!
« Reply #1 on: November 20, 2009, 02:41:24 pm »
  • (y)(;>.<;)(y)
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3293
ship1 = load_bitmap( "ship1.png", NULL );
Take this line out of the header and put it into your main function.
« Last Edit: November 20, 2009, 02:43:39 pm by TheDarkJay »
Logged

PoeFacedKilla

Prussian Killer Bee
Re: [C++] Help!
« Reply #2 on: November 20, 2009, 02:42:58 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 269
now I get an error with the
Code: [Select]
int main( int argc, char *argv[] )
Logged
the Indyboard - User Generated Social Forum | Now With Even More Discussion!
Poe, The Independent Programmer
  • Zelda Shrine
Re: [C++] Help!
« Reply #3 on: November 20, 2009, 02:44:28 pm »
  • (y)(;>.<;)(y)
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3293
What is the error?
Logged

PoeFacedKilla

Prussian Killer Bee
Re: [C++] Help!
« Reply #4 on: November 20, 2009, 02:46:20 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 269
expected , or ; before int
Logged
the Indyboard - User Generated Social Forum | Now With Even More Discussion!
Poe, The Independent Programmer
  • Zelda Shrine
Re: [C++] Help!
« Reply #5 on: November 20, 2009, 03:00:48 pm »
  • (y)(;>.<;)(y)
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3293
BITMAP *ship1 = NULL

should be

BITMAP *ship1 = NULL;

missed that first time I was looking at it.
Logged

PoeFacedKilla

Prussian Killer Bee
Re: [C++] Help!
« Reply #6 on: November 20, 2009, 03:05:46 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 269
yeah i did too..
Logged
the Indyboard - User Generated Social Forum | Now With Even More Discussion!
Poe, The Independent Programmer
  • Zelda Shrine
Re: [C++] Help!
« Reply #7 on: November 20, 2009, 03:45:38 pm »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6629
BITMAP *ship1 = NULL;

Should actually be;

extern BITMAP *ship1;

and should be declared in header.h and initializied in main.cpp.

Header files are only for declarations, they should never include actual code.
Logged

Antidote

>.>
Re: [C++] Help!
« Reply #8 on: November 26, 2009, 12:43:05 am »
  • In all seriousness who's serious?
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1485
unless the code is ridiculously small, but even then it should be avoided however if you decide to go the OOP way then

Code: [Select]
public:
    MyClass() {};
    ~MyClass() {};
is common and acceptable. ctors and dtors don't always DO anything and don't need code however you still need to declare them and this allows you to do so with out polluting your code files.
Logged
  • Axiomatic Data Laboratories
Re: [C++] Help!
« Reply #9 on: December 01, 2009, 09:32:28 am »
  • Minalien
  • *
  • Reputation: +10/-1
  • Offline Offline
  • Gender: Female
  • Posts: 2119
Try this:

header.h
Code: [Select]
// Header.h

#ifndef _HEADER_H_
#define _HEADER_H_

// Headers
#include <allegro.h>

// External Variable Declarations
extern BITMAP* ship1;

// Function Prototypes
int main(int, char**);

#endif

Main.cpp
Code: [Select]
// Main.cpp

// Headers
#include "Header.h"

// Variable Definitions (defining ship1 in Header.h allows other source files to access the (global) variable after by including Header.h in them.
BITMAP *ship1 = NULL;

int main(int argc, char **argv)
{
      // Initialize Allegro
      allegro_init(); // Allegro system Init
      install_keyboard(); // Allegro Keyboard Init

      // Configure the Allegro window
      set_window_title("SPACEWAR"); // Window Title
      set_color_depth(24); // Color Depth
      set_gfx_mode(GFX_AUTODETECT, // Graphics Mode (Auto-Detect)
            680, 420,       // Screen Size
            0, 0);            // Virtual Screen Size

      // Load the Ship bitmap
      ship1 = load_bitmap("ship1.png", NULL);

      // If Escape wasn't pressed, draw the sprite
      if(!(KEY_ESC))
            draw_sprite(ship1, 0, 0);

      // Wait for a key press (since we're foolishly not using a game loop)
      readkey();

      // Destroy the ship
      destroy_bitmap(ship1);

      return 0;
}
« Last Edit: December 01, 2009, 09:36:59 am by MiNalien »
Logged
Quote
There's such a double standard about religion in the modern world. Catholics can gather, wear white robes, and say "In nomine Patris, et Filii, et Spiritus Sancti" and be considered normal.

But if my friends and I gather, wear black robes, and say  "Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn", we're considered cultists.
  • Development Blog

Antidote

>.>
Re: [C++] Help!
« Reply #10 on: December 04, 2009, 07:09:22 am »
  • In all seriousness who's serious?
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1485
Quote from: Code
      // Wait for a key press (since we're foolishly not using a game loop)
...You know i just noticed that <.<
Logged
  • Axiomatic Data Laboratories
Pages: [1]   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.048 seconds with 75 queries.

anything