Colors messed up

Nov 1, 2019 at 12:53pm
Hi i was writing a graphics library but when i use it colors get mixed up

This code needed to load my custom image format to console application.

Don 't say why you don't use sdl
Reason:
i don't want it beacuse its hard to learn but if you write your own api you will be always know what code what does.Also i love making everything zero also i writing a physics libary for it.
Sorry about grammar i was from turkey so i don't know english much. :3

Also everyone said writing your graphics library requires lot of math but i don't think so. I just used console and i managed to reach this level.

I can create basic map and play on it with really unrealistic physics lol. For example object are not rotating when pushed only stays normal.
Library is useful.


Some meanings of turkish words: renk = color git= go resim=image others are just randomized text symbol = sembol resimverisi=imagedata(?) KIRMIZI etc. are colors kare=square
That was from my graphics libary:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void prePiksel(char x, char y, const unsigned char SEMBOL,int renk)
{
	
	
    
	
	SetConsoleTextAttribute(hConsole, renk);
	preGitxy(x, y);
	SetConsoleTextAttribute(hConsole, renk);
	cout << SEMBOL;


	

}




This from image loader:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
  #pragma once
#include "pch.h"
#include <iostream>
#include <fstream>
#include <string>
#include "2d.h"

using namespace std;

class Resma //save manager
{
	
public:
	void ColorImage(const char * fname, string resimverisi,int x ,int y)
	{
		ifstream resim(fname);
		
		

		if (resim.is_open())
		{
			while (getline(resim, resimverisi))
			{
				for (unsigned i = 0; i < resimverisi.length(); ++i)
				{
				
				
		
				if (resimverisi.find('K') != std::string::npos) {

				
					prePiksel(i, + 1, KARE, KIRMIZI);
					

				}
				if (resimverisi.find('Y') != std::string::npos) {

					
					prePiksel(i, + 1, KARE, YESIL);
					

				}
				if (resimverisi.find('M') != std::string::npos) {


					prePiksel(i,  + 1, KARE, MOR);
					

				}
				if (resimverisi.find('E') != std::string::npos) {


					prePiksel(i, + 1, KARE, MAVI);
					
					
				}
				if (resimverisi.find('A') != std::string::npos) {


					prePiksel(i, + 1, KARE, ALTIN);


				}
				if (resimverisi.find('STIR') != std::string::npos) {


					cout << "\n";


				}
				}
			}
			resim.close();
		}
	}
	
};

Resma resle;


My file:
K K K
Y Y Y
Last edited on Nov 1, 2019 at 1:03pm
Nov 1, 2019 at 2:50pm
i can't figure out
Nov 1, 2019 at 3:36pm
Hi,
it's hard for everyone in this forum reading your code if you don't use englisch names for your designators. Your lack of English language ability is no excusing since there exist lots of online dictionaries.

Coming to your problem:
What's the (logical) error of your program? (What output do you get, what output do you expect?)
It would also be helpful if you could provide us a compilable and runnable piece of your code.
Nov 1, 2019 at 6:21pm
its compilable

I want if my file contains K a red square
if its y a green square
if its m a blue square

Logical error was i was expecting

Input like this . ███
but not this

for example my file have these values:
KKYY
i can see only y(green color)
no k (red color)

that was problem of it.
Colors mixes .
If i put ALTN (gold color) it will be only put gold color not other colors like problems.
Nov 1, 2019 at 6:26pm
its compilable

No it isn't. There's no main function.
Nov 1, 2019 at 6:52pm
its compilable

I haven’t got a clue of what your code should do, but this instruction is definitely wrong:
 
if (resimverisi.find('STIR') != std::string::npos) {

And your compiler should raise an error on that (single quotes are for characters, not for strings).

With g++:
warning: multi-character character constant [-Wmultichar]
    8 |     if (resimverisi.find('STIR') != std::string::npos) {
      |                          ^~~~~~


So your code:
- misses a main(), as MikeyBoy said
- includes an unknown "2d.h" header which you don’t provide
- contains at least one error
Take a breath and ask yourself: are you doing your best to get an useful answer? ;-)
Nov 1, 2019 at 7:02pm
If the line contains "K Y M E A" then all of your if statements will be true and you'll write different colors to the same pixel one after another. This probably isn't want you intended.

Don't

Double


or


triple


space

your


code.

You'll wear out the PageUp and PageDown keys on your keyboard. :)
Nov 1, 2019 at 7:17pm
beacuse this is not the main header.
Main cpp was another file that only have two commands:
preBaslat();
preResimprei("test.txt",0,0);

2d.h was my graphics header.(my own graphics library header)
just have basic commands squares lines circles etc.
prePiksel was one of it i said.

You can easly implement prePiksel by copying first code.

Nov 1, 2019 at 8:44pm
my expected output was colored squares but it doesnt work . No compiler errors.
Compiles on visual studio 2017 i was not using mingw compiler
Nov 1, 2019 at 8:54pm
You can easly implement prePiksel by copying first code.
No offense, but no we can't. Some people on this forum are very generous, but within reason. Unless someone can immediately detect the issue by looking at your posts, chances are they aren't going to laboriously figure out exactly what's going wrong through guesswork.

An art to problem solving is understanding how to break a problem down. Chances are, your program as a whole isn't wrong. But a specific part of it is. Make a new project, and keep simplifying your code so that you can get to a minimal example that still demonstrates the issue you're having, small enough so that someone reading might actually attempt to understand it.
Nov 1, 2019 at 9:46pm
preGitxy actually a gotoxy code...

My question about the file format and code snippet.
Not about project.

My code compiles its just a bug about colors mix problem.
How i can fix it.
Maybe problem in file but i removed all spaces from it still same bug.

Please don't change subject of thread.


Nov 2, 2019 at 8:56am
how i can fix this?
Nov 2, 2019 at 9:40am
I've tried rewrite code and i used an a for loop but whatever happeneded the application stopped working :
#pragma once
#include "pch.h"
#include <iostream>
#include <fstream>
#include <string>
#include "2d.h"

using namespace std;

class ColorImage //save manager
{
// Access specifier
public:
void preResimprei(const char * fname, string resimverisi,int x ,int y,int sizex)
{
ifstream resim(fname);



if (resim.is_open())
{
while (getline(resim, resimverisi))
{
for (int a = 0; a < sizex; a = a + 1) {
if (resimverisi.find("K") != std::string::npos) {
prePiksel(x + a, y, KARE, KIRMIZI);
}
if (resimverisi.find("Y") != std::string::npos) {
prePiksel(x + a, y, KARE, YESIL);
}

}

}
resim.close();
}
}

};

ColorImage resle;
Last edited on Nov 2, 2019 at 9:41am
Topic archived. No new replies allowed.