Dark Bit Factory & Gravity

PROGRAMMING => C / C++ /C# => Topic started by: va!n on May 28, 2015

Title: Code Design - splitted project parts
Post by: va!n 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];

Title: Re: Code Design - splitted project parts
Post by: spitfire 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"
Title: Re: Code Design - splitted project parts
Post by: va!n 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++
Title: Re: Code Design - splitted project parts
Post by: Clyde 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.
Title: Re: Code Design - splitted project parts
Post by: Knurz 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.
Title: Re: Code Design - splitted project parts
Post by: hellfire 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.
Title: Re: Code Design - splitted project parts
Post by: va!n on July 03, 2015
Thanks for the reply.
hellfire is right, his example is exactly what i am looking for.

 :goodpost: