Author Topic: Code Design - splitted project parts  (Read 1864 times)

0 Members and 1 Guest are viewing this topic.

Offline va!n

  • Pentium
  • *****
  • Posts: 1398
  • Karma: 108
    • View Profile
    • http://www.secretly.de
Code Design - splitted project parts
« on: May 28, 2015 »
Atm i write all the code of my test projects in just one cpp (the main cpp) file, which really suxx. I would like to have the main.cpp (creating windows/GUI and handling messages)... and have seperate .cpp files for all the FilterEffects, another .cpp for all the AdjustmentFunctions and so on.

How to use a buffer (storing image datas as 1D array) and access this buffer from FilterEffects.cpp and from AdjustFunctions.cpp?
I know this may be some very basic stuff but i would be really glad to get some teaching / help, how to realize this.  8)

Code: [Select]
Atm i am using this way...

    static long bufferColors[360 * 256];

« Last Edit: May 28, 2015 by va!n »
- hp EliteBook 8540p, 4 GB RAM, Windows 8.1 x64
- Asus P5Q, Intel Q8200, 6 GB DDR2, Radeon 4870, Windows 8.1 x64
http://www.secretly.de
Challenge Trophies Won:

Offline spitfire

  • Amiga 1200
  • ****
  • Posts: 272
  • Karma: 9
    • View Profile
Re: Code Design - splitted project parts
« Reply #1 on: May 29, 2015 »
cpp files are often paired with a .h file of the same name. In that .h file you declare stuff you want to share and then include it into other .h or .cpp files.

in a new file data.h:
static long bufferColors[360 * 256];

Then add this to every file where you will use it

#include "data.h"

Offline va!n

  • Pentium
  • *****
  • Posts: 1398
  • Karma: 108
    • View Profile
    • http://www.secretly.de
Re: Code Design - splitted project parts
« Reply #2 on: June 25, 2015 »
@spitfire:
Thanks a lot for your very helpfull answer and great explation/example !!  :cheers:
I mangaed to get it work  :updance:
K++
- hp EliteBook 8540p, 4 GB RAM, Windows 8.1 x64
- Asus P5Q, Intel Q8200, 6 GB DDR2, Radeon 4870, Windows 8.1 x64
http://www.secretly.de
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: Code Design - splitted project parts
« Reply #3 on: June 30, 2015 »
What I found quite useful is when using headers is to use a header guard, as often I would get compile errors saying that variables and functions had already been declared.
All you need do is surround your code with:
Code: [Select]
#ifndef _NAME_OF_HEADER_H_
#define _NAME_OF_HEADER_H_

#endif

btw, the _H_ is optional.

hope it helps.
-Clyde.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Knurz

  • Atari ST
  • ***
  • Posts: 100
  • Karma: 24
    • View Profile
Re: Code Design - splitted project parts
« Reply #4 on: July 01, 2015 »
What I found quite useful is when using headers is to use a header guard, as often I would get compile errors saying that variables and functions had already been declared.
All you need do is surround your code with:
Code: [Select]
#ifndef _NAME_OF_HEADER_H_
#define _NAME_OF_HEADER_H_

#endif

btw, the _H_ is optional.

hope it helps.
-Clyde.

Optional you can use #pragma once which should be compiled by the most (modern) Compilers.
Remember what the dormouse said: Feed your head

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1289
  • Karma: 466
    • View Profile
    • my stuff
Re: Code Design - splitted project parts
« Reply #5 on: July 03, 2015 »
in a new file data.h:
static long bufferColors[360 * 256];

Then add this to every file where you will use it

#include "data.h"

This way every cpp-file which includes data.h will create its own buffer.
But I guess what Vain asked for is a single shared buffered like this:
Code: [Select]
// mybuffer.cpp
long buffer[360*256];

Code: [Select]
// mybuffer.h
#pragma once
extern long buffer[320*256];

Code: [Select]
// mycode1.cpp
#include "mybuffer.h"

Code: [Select]
// mycode2.cpp
#include "mybuffer.h"

This way mycode1 & mycode2 will work on the same "buffer" from mybuffer.cpp.
Challenge Trophies Won:

Offline va!n

  • Pentium
  • *****
  • Posts: 1398
  • Karma: 108
    • View Profile
    • http://www.secretly.de
Re: Code Design - splitted project parts
« Reply #6 on: July 03, 2015 »
Thanks for the reply.
hellfire is right, his example is exactly what i am looking for.

 :goodpost:
- hp EliteBook 8540p, 4 GB RAM, Windows 8.1 x64
- Asus P5Q, Intel Q8200, 6 GB DDR2, Radeon 4870, Windows 8.1 x64
http://www.secretly.de
Challenge Trophies Won: