How to generate complimentary SPWM using ATmega328 in Arduino IDE - c++

Here generated complimentary PWM using ATmega328 in Arduino IDE.
The given code works well. It is generating complimentary PWM output but the problem is that one output is as desired, but the complimentary output is not as desired. Initially complimentary pin should be on HIGH and after SPWM cycles from lookUp table it should be HIGH forever till next pulse, never go for LOW. The other output is as desired, it is initially LOW and after SPWM pulses from lookUp it remains LOW till next pulses. I need to generate pulses as I attached. Also want to add deadtime.
#define MAX_COUNT 500 // (16 MHz / SW_FREQ) / 2
#define NO_OF_PULSES 160 // 16 kHz / 50 Hz = 320; 320 / 2 = 160 pulses in one half-cycle
int lookUp[NO_OF_PULSES] = {500, 490, 480, 471, 461, 451, 441, 431, 422, 412,
402, 393, 383, 374, 364, 355, 345, 336, 327, 318, 309, 300, 291,
282, 273, 264, 256, 247, 239, 230, 222, 214, 206, 198, 190, 183,
175, 168, 161, 153, 146, 140, 133, 126, 120, 113, 107, 101, 95,
90, 84, 79, 74, 69, 64, 59, 54, 50, 46, 42, 38, 34, 31, 28, 24,
22, 19, 16, 14, 12, 10, 8, 6, 5, 3, 2, 2, 1, 0, 0, 0, 0, 0, 1,
2, 2, 3, 5, 6, 8, 10, 12, 14, 16, 19, 22, 24, 28, 31, 34, 38,
42, 46, 50, 54, 59, 64, 69, 74, 79, 84, 90, 95, 101, 107, 113,
120, 126, 133, 140, 146, 153, 161, 168, 175, 183, 190, 198, 206,
214, 222, 230, 239, 247, 256, 264, 273, 282, 291, 300, 309, 318,
327, 336, 345, 355, 364, 374, 383, 393, 402, 412, 422, 431, 441,
451, 461, 471, 480, 490
};
volatile bool positiveHalf = true;
volatile bool zeroCrossing = false;
void setup() {
pinMode(9 , OUTPUT); // Pin 09 for PWM signal A
pinMode(10, OUTPUT); // Pin 10 for PWM signal B
TCCR1A = 0;
TCCR1B = 0;
TCCR1A = 0b11100000; // Set timer 1 to phase and frequency correct PWM mode (mode 8)
TCCR1B = 0b00010001; // Set no prescaler
ICR1 = MAX_COUNT; // Period for 16MHz crystal, for a switching frequency of 16 kHz for 320 subdevisions per 50 Hz sin wave cycle.
OCR1A = lookUp[0]; // Set top value for timer 1
OCR1B = lookUp[0];
TIMSK1 |= _BV(TOIE1); // Enable timer 1 overflow interrupt
sei();
}
ISR(TIMER1_OVF_vect) {
static int i = 1;
if (i >= NO_OF_PULSES) { // NO_OF_PULSES
i = 0;
}
OCR1A = lookUp[i];
OCR1B = lookUp[i];
if (i == 1) {
positiveHalf = !positiveHalf;
TCCR1A = positiveHalf ? 0b11100000 : 0b00000000; // +Ve half: Set OC1A (pin D9) on Compare Match; // Disconnect OC1B (pin D10); -Ve half: Opposite
}
i++;
}
void loop() {
}

Related

On the confusion of rgb numerical comparison

I wrote a program that gives any RGB value and can find similar colors from the program.
But the result is a little confusing.
The program is a little long, mainly to fill in some RGB data.
The main key points are as follows
//MyColor.h
#pragma once
#ifndef __MYCOLOR_H__
#define __MYCOLOR_H__
#include <windef.h>
#include <wingdi.h>
#include <vector>
namespace my
{
struct ColorData
{
UINT8 family;
UINT16 id;
const char * name;
const char * hex;
UINT8 rgb[3];
ColorData(UINT8 _family, UINT16 _id, const char *_name, UINT8 _r, UINT8 _g, UINT8 _b, const char * _hex)
{
family = _family;
id = _id;
name = _name;
hex = _hex;
rgb[0] = _r;
rgb[1] = _g;
rgb[2] = _b;
}
ColorData(){};
};
/*
MyColorSpace::myColorFamily cc;
MyColorSpace::myColorFamily::data *da;
da = cc.Get(cc.family_blue, cc.blue_deepsky);
da = cc.Get(cc.family_green, cc.green_yellowGreen);
da = cc.Get(cc.family_orange, cc.orange_tomato);
debug(("name,hex,rgb:<%s>,<%s>,[%d,%d,%d]\n",da->name,da->hex,(da->rgb)[0],(da->rgb)[1],(da->rgb)[2]));
*/
class ColorFamily {
public:
enum family
{
family_gray = 0,
family_red = 1,
family_orange = 2,
family_brown = 3,
family_blue = 4,
family_green = 5,
family_pink = 6,
family_purple = 7,
family_white = 8,
family_yellow = 9
};
enum gray {
gray_gainsboro = 0,
gray_light = 1,
gray_silver = 2,
gray_dark = 3,
gray_ = 4,
gray_dim = 5,
gray_lightslate = 6,
gray_slate = 7,
gray_darkslate = 8,
gray_black = 9
};
enum red {
red_indian = 0,
red_lightcoral = 1,
red_salmon = 2,
red_darksalmon = 3,
red_lightsalmon = 4,
red_crimson = 5,
red_ = 6,
red_firebrick = 7,
red_dark = 8
};
enum orange {
orange_lightsalmon = 0,
orange_coral = 1,
orange_tomato = 2,
orange_red = 3,
orange_dark = 4,
orange_ = 5
};
enum brown {
brown_cornsilk = 0,
brown_blanchedalmond = 1,
brown_bisque = 2,
brown_navajowhite = 3,
brown_wheat = 4,
brown_burlywood = 5,
brown_tan = 6,
brown_rosy = 7,
brown_sandy = 8,
brown_goldenrod = 9,
brown_darkgoldenrod = 10,
brown_peru = 11,
brown_chocolate = 12,
brown_saddle = 13,
brown_sienna = 14,
brown_ = 15,
brown_maroon = 16
};
enum blue {
blue_aqua = 0,
blue_cyan = 1,
blue_lightcyan = 2,
blue_paleturquoise = 3,
blue_aquamarine = 4,
blue_turquoise = 5,
blue_mediumturquoise = 6,
blue_darkturquoise = 7,
blue_cadet = 8,
blue_steel = 9,
blue_lightsteel = 10,
blue_powder = 11,
blue_light = 12,
blue_sky = 13,
blue_lightsky = 14,
blue_deepsky = 15,
blue_dodger = 16,
blue_cornflower = 17,
blue_mediumslate = 18,
blue_royal = 19,
blue_ = 20,
blue_medium = 21,
blue_dark = 22,
blue_navy = 23,
blue_midnight = 24
};
enum green {
green_yellow = 0,
green_chartreuse = 1,
green_lawn = 2,
green_lime = 3,
green_limeGreen = 4,
green_pale = 5,
green_light = 6,
green_mediumspring = 7,
green_spring = 8,
green_mediumsea = 9,
green_sea = 10,
green_forest = 11,
green_ = 12,
green_dark = 13,
green_yellowGreen = 14,
green_olivedrab = 15,
green_olive = 16,
green_darkolive = 17,
green_mediumaquamarine = 18,
green_darksea = 19,
green_lightsea = 20,
green_darkcyan = 21,
green_teal = 22
};
enum pink {
pink_ = 0,
pink_light = 1,
pink_hot = 2,
pink_deep = 3,
pink_mediumvioletred = 4,
pink_palevioletred = 5
};
enum purple {
purple_lavender = 0,
purple_thistle = 1,
purple_plum = 2,
purple_violet = 3,
purple_orchid = 4,
purple_fuchsia = 5,
purple_magenta = 6,
purple_mediumorchid = 7,
purple_medium = 8,
purple_rebecca = 9,
purple_blueviolet = 10,
purple_darkviolet = 11,
purple_darkorchid = 12,
purple_darkmagenta = 13,
purple_ = 14,
purple_indigo = 15,
purple_slateblue = 16,
purple_darkslateblue = 17,
purple_mediumslateblue = 18
};
enum white {
white_ = 0,
white_snow = 1,
white_honeydew = 2,
white_mintcream = 3,
white_azure = 4,
white_aliceblue = 5,
white_ghost = 6,
white_smoke = 7,
white_seashell = 8,
white_beige = 9,
white_oldlace = 10,
white_floral = 11,
white_ivory = 12,
white_antique = 13,
white_linen = 14,
white_lavenderblush = 15,
white_mistyrose = 16
};
enum yellow {
yellow_gold = 0,
yellow_ = 1,
yellow_light = 2,
yellow_lemonchiffon = 3,
yellow_lightgoldenrod = 4,
yellow_papayawhip = 5,
yellow_moccasin = 6,
yellow_peachpuff = 7,
yellow_palegoldenrod = 8,
yellow_khaki = 9,
yellow_darkkhaki = 10
};
ColorFamily();
~ColorFamily() = default;
private:
ColorData * _detail;
public:
std::vector<ColorData>List;
ColorData* Get(const char * name);
ColorData* Get(UINT8 _family, UINT16 _id);
bool Get(UINT8 _r,UINT8 _g,UINT8 _b,ColorData &_out);
void FillColorData(ColorData &tar,ColorData* src);
};
}
#endif
//MyColor.cpp
#include "MyColor.h"
#include "stdlib.h"
#include "debug.h"
#define Less10(a,b) abs(a-b) < 10 ? true : false
namespace my
{
ColorFamily::ColorFamily()
{
//data da("",20,30,40,"#hex")
//List.push_back(data(0,"name", 20, 30, 40, "#hex"));
//gray
//gray
List.push_back(ColorData(0, 0, "gray_gainsboro", 220, 220, 220, "#dcdcdc"));
List.push_back(ColorData(0, 1, "gray_light", 211, 211, 211, "#d3d3d3"));
List.push_back(ColorData(0, 2, "gray_silver", 192, 192, 192, "#c0c0c0"));
List.push_back(ColorData(0, 3, "gray_dark", 169, 169, 169, "#a9a9a9"));
List.push_back(ColorData(0, 4, "gray_", 128, 128, 128, "#808080"));
List.push_back(ColorData(0, 5, "gray_dim", 105, 105, 105, "#696969"));
List.push_back(ColorData(0, 6, "gray_lightslate", 119, 136, 153, "#778899"));
List.push_back(ColorData(0, 7, "gray_slate", 112, 128, 144, "#708090"));
List.push_back(ColorData(0, 8, "gray_darkslate", 47, 79, 79, "#2f4f4f"));
List.push_back(ColorData(0, 9, "gray_black", 0, 0, 0, "#000000"));
//red
List.push_back(ColorData(1, 0, "red_indian", 205, 92, 92, "#cd5c5c"));
List.push_back(ColorData(1, 1, "red_lightcoral", 240, 128, 128, "#f08080"));
List.push_back(ColorData(1, 2, "red_salmon", 250, 128, 114, "#fa8072"));
List.push_back(ColorData(1, 3, "red_darksalmon", 233, 150, 122, "#e9967a"));
List.push_back(ColorData(1, 4, "red_lightsalmon", 255, 160, 122, "#ffa07a"));
List.push_back(ColorData(1, 5, "red_crimson", 220, 20, 60, "#dc143c"));
List.push_back(ColorData(1, 6, "red_", 255, 0, 0, "#ff0000"));
List.push_back(ColorData(1, 7, "red_firebrick", 178, 34, 34, "#b22222"));
List.push_back(ColorData(1, 8, "red_dark", 139, 0, 0, "#8b0000"));
//orange
List.push_back(ColorData(2, 0, "orange_lightsalmon", 255, 160, 122, "#ffa07a"));
List.push_back(ColorData(2, 1, "orange_coral", 255, 127, 80, "#ff7f50"));
List.push_back(ColorData(2, 2, "orange_tomato", 255, 99, 71, "#ff6347"));
List.push_back(ColorData(2, 3, "orange_red", 255, 69, 0, "#ff4500"));
List.push_back(ColorData(2, 4, "orange_dark", 255, 140, 0, "#ff8c00"));
List.push_back(ColorData(2, 5, "orange_", 255, 165, 0, "#ffa500"));
//brown
List.push_back(ColorData(3, 0, "brown_cornsilk", 255, 248, 220, "#fff8dc"));
List.push_back(ColorData(3, 1, "brown_blanchedalmond", 255, 235, 205, "#ffebcd"));
List.push_back(ColorData(3, 2, "brown_bisque", 255, 228, 196, "#ffe4c4"));
List.push_back(ColorData(3, 3, "brown_navajowhite", 255, 222, 173, "#ffdead"));
List.push_back(ColorData(3, 4, "brown_wheat", 245, 222, 179, "#f5deb3"));
List.push_back(ColorData(3, 5, "brown_burlywood", 222, 184, 135, "#deb887"));
List.push_back(ColorData(3, 6, "brown_tan", 210, 180, 140, "#d2b48c"));
List.push_back(ColorData(3, 7, "brown_rosy", 188, 143, 143, "#bc8f8f"));
List.push_back(ColorData(3, 8, "brown_sandy", 244, 164, 96, "#f4a460"));
List.push_back(ColorData(3, 9, "brown_goldenrod", 218, 165, 32, "#daa520"));
List.push_back(ColorData(3, 10, "brown_darkgoldenrod", 184, 134, 11, "#b8860b"));
List.push_back(ColorData(3, 11, "brown_peru", 205, 133, 63, "#cd853f"));
List.push_back(ColorData(3, 12, "brown_chocolate", 210, 105, 30, "#d2691e"));
List.push_back(ColorData(3, 13, "brown_saddle", 139, 69, 19, "#8b4513"));
List.push_back(ColorData(3, 14, "brown_sienna", 160, 82, 45, "#a0522d"));
List.push_back(ColorData(3, 15, "brown_", 165, 42, 42, "#a52a2a"));
List.push_back(ColorData(3, 16, "brown_maroon", 128, 0, 0, "#800000"));
//blue
List.push_back(ColorData(4, 0, "blue_aqua", 0, 255, 255, "#00ffff"));
List.push_back(ColorData(4, 1, "blue_cyan", 0, 255, 255, "#00ffff"));
List.push_back(ColorData(4, 2, "blue_lightcyan", 224, 255, 255, "#e0ffff"));
List.push_back(ColorData(4, 3, "blue_paleturquoise", 175, 238, 238, "#afeeee"));
List.push_back(ColorData(4, 4, "blue_aquamarine", 127, 255, 212, "#7fffd4"));
List.push_back(ColorData(4, 5, "blue_turquoise", 64, 224, 208, "#40e0d0"));
List.push_back(ColorData(4, 6, "blue_mediumturquoise", 72, 209, 204, "#48d1cc"));
List.push_back(ColorData(4, 7, "blue_darkturquoise", 0, 206, 209, "#00ced1"));
List.push_back(ColorData(4, 8, "blue_cadet", 95, 158, 160, "#5f9ea0"));
List.push_back(ColorData(4, 9, "blue_steel", 70, 130, 180, "#4682b4"));
List.push_back(ColorData(4, 10, "blue_lightsteel", 176, 196, 222, "#b0c4de"));
List.push_back(ColorData(4, 11, "blue_powder", 176, 224, 230, "#b0e0e6"));
List.push_back(ColorData(4, 12, "blue_light", 173, 216, 230, "#add8e6"));
List.push_back(ColorData(4, 13, "blue_sky", 135, 206, 235, "#87ceeb"));
List.push_back(ColorData(4, 14, "blue_lightsky", 135, 206, 250, "#87cefa"));
List.push_back(ColorData(4, 15, "blue_deepsky", 0, 191, 255, "#00bfff"));
List.push_back(ColorData(4, 16, "blue_dodger", 30, 144, 255, "#1e90ff"));
List.push_back(ColorData(4, 17, "blue_cornflower", 100, 149, 237, "#6495ed"));
List.push_back(ColorData(4, 18, "blue_mediumslate", 123, 104, 238, "#7b68ee"));
List.push_back(ColorData(4, 19, "blue_royal", 65, 105, 225, "#4169e1"));
List.push_back(ColorData(4, 20, "blue_", 0, 0, 255, "#0000ff"));
List.push_back(ColorData(4, 21, "blue_medium", 0, 0, 205, "#0000cd"));
List.push_back(ColorData(4, 22, "blue_dark", 0, 0, 139, "#00008b"));
List.push_back(ColorData(4, 23, "blue_navy", 0, 0, 128, "#000080"));
List.push_back(ColorData(4, 24, "blue_midnight", 25, 25, 112, "#191970"));
//green
List.push_back(ColorData(5, 0, "green_yellow", 173, 255, 47, "#adff2f"));
List.push_back(ColorData(5, 1, "green_chartreuse", 127, 255, 0, "#7fff00"));
List.push_back(ColorData(5, 2, "green_lawn", 124, 252, 0, "#7cfc00"));
List.push_back(ColorData(5, 3, "green_lime", 0, 255, 0, "#00ff00"));
List.push_back(ColorData(5, 4, "green_lime", 50, 205, 50, "#32cd32"));
List.push_back(ColorData(5, 5, "green_pale", 152, 251, 152, "#98fb98"));
List.push_back(ColorData(5, 6, "green_light", 144, 238, 144, "#90ee90"));
List.push_back(ColorData(5, 7, "green_mediumspring", 0, 250, 154, "#00fa9a"));
List.push_back(ColorData(5, 8, "green_spring", 0, 255, 127, "#00ff7f"));
List.push_back(ColorData(5, 9, "green_mediumsea", 60, 179, 113, "#3cb371"));
List.push_back(ColorData(5, 10, "green_sea", 46, 139, 87, "#2e8b57"));
List.push_back(ColorData(5, 11, "green_forest", 34, 139, 34, "#228b22"));
List.push_back(ColorData(5, 12, "green_", 0, 128, 0, "#008000"));
List.push_back(ColorData(5, 13, "green_dark", 0, 100, 0, "#006400"));
List.push_back(ColorData(5, 14, "green_yellow", 154, 205, 50, "#9acd32"));
List.push_back(ColorData(5, 15, "green_olivedrab", 107, 142, 35, "#6b8e23"));
List.push_back(ColorData(5, 16, "green_olive", 128, 128, 0, "#808000"));
List.push_back(ColorData(5, 17, "green_darkolive", 85, 107, 47, "#556b2f"));
List.push_back(ColorData(5, 18, "green_mediumaquamarine", 102, 205, 170, "#66cdaa"));
List.push_back(ColorData(5, 19, "green_darksea", 143, 188, 139, "#8fbc8b"));
List.push_back(ColorData(5, 20, "green_lightsea", 32, 178, 170, "#20b2aa"));
List.push_back(ColorData(5, 21, "green_darkcyan", 0, 139, 139, "#008b8b"));
List.push_back(ColorData(5, 22, "green_teal", 0, 128, 128, "#008080"));
//pink
List.push_back(ColorData(6, 0, "pink_", 255, 192, 203, "#ffc0cb"));
List.push_back(ColorData(6, 1, "pink_light", 255, 182, 193, "#ffb6c1"));
List.push_back(ColorData(6, 2, "pink_hot", 255, 105, 180, "#ff69b4"));
List.push_back(ColorData(6, 3, "pink_deep", 255, 20, 147, "#ff1493"));
List.push_back(ColorData(6, 4, "pink_mediumvioletred", 199, 21, 133, "#c71585"));
List.push_back(ColorData(6, 5, "pink_palevioletred", 219, 112, 147, "#db7093"));
//purple
List.push_back(ColorData(7, 0, "purple_lavender", 230, 230, 250, "#e6e6fa"));
List.push_back(ColorData(7, 1, "purple_thistle", 216, 191, 216, "#d8bfd8"));
List.push_back(ColorData(7, 2, "purple_plum", 221, 160, 221, "#dda0dd"));
List.push_back(ColorData(7, 3, "purple_violet", 238, 130, 238, "#ee82ee"));
List.push_back(ColorData(7, 4, "purple_orchid", 218, 112, 214, "#da70d6"));
List.push_back(ColorData(7, 5, "purple_fuchsia", 255, 0, 255, "#ff00ff"));
List.push_back(ColorData(7, 6, "purple_magenta", 255, 0, 255, "#ff00ff"));
List.push_back(ColorData(7, 7, "purple_mediumorchid", 186, 85, 211, "#ba55d3"));
List.push_back(ColorData(7, 8, "purple_medium", 147, 112, 219, "#9370db"));
List.push_back(ColorData(7, 9, "purple_rebecca", 102, 51, 153, "#663399"));
List.push_back(ColorData(7, 10, "purple_blueviolet", 138, 43, 226, "#8a2be2"));
List.push_back(ColorData(7, 11, "purple_darkviolet", 148, 0, 211, "#9400d3"));
List.push_back(ColorData(7, 12, "purple_darkorchid", 153, 50, 204, "#9932cc"));
List.push_back(ColorData(7, 13, "purple_darkmagenta", 139, 0, 139, "#8b008b"));
List.push_back(ColorData(7, 14, "purple_", 128, 0, 128, "#800080"));
List.push_back(ColorData(7, 15, "purple_indigo", 75, 0, 130, "#4b0082"));
List.push_back(ColorData(7, 16, "purple_slateblue", 106, 90, 205, "#6a5acd"));
List.push_back(ColorData(7, 17, "purple_darkslateblue", 72, 61, 139, "#483d8b"));
List.push_back(ColorData(7, 18, "purple_mediumslateblue", 123, 104, 238, "#7b68ee"));
//white
List.push_back(ColorData(8, 0, "white_", 255, 255, 255, "#ffffff"));
List.push_back(ColorData(8, 1, "white_snow", 255, 250, 250, "#fffafa"));
List.push_back(ColorData(8, 2, "white_honeydew", 240, 255, 240, "#f0fff0"));
List.push_back(ColorData(8, 3, "white_mintcream", 245, 255, 250, "#f5fffa"));
List.push_back(ColorData(8, 4, "white_azure", 240, 255, 255, "#f0ffff"));
List.push_back(ColorData(8, 5, "white_aliceblue", 240, 248, 255, "#f0f8ff"));
List.push_back(ColorData(8, 6, "white_ghost", 248, 248, 255, "#f8f8ff"));
List.push_back(ColorData(8, 7, "white_smoke", 245, 245, 245, "#f5f5f5"));
List.push_back(ColorData(8, 8, "white_seashell", 255, 245, 238, "#fff5ee"));
List.push_back(ColorData(8, 9, "white_beige", 245, 245, 220, "#f5f5dc"));
List.push_back(ColorData(8, 10, "white_oldlace", 253, 245, 230, "#fdf5e6"));
List.push_back(ColorData(8, 11, "white_floral", 255, 250, 240, "#fffaf0"));
List.push_back(ColorData(8, 12, "white_ivory", 255, 255, 240, "#fffff0"));
List.push_back(ColorData(8, 13, "white_antique", 250, 235, 215, "#faebd7"));
List.push_back(ColorData(8, 14, "white_linen", 250, 240, 230, "#faf0e6"));
List.push_back(ColorData(8, 15, "white_lavenderblush", 255, 240, 245, "#fff0f5"));
List.push_back(ColorData(8, 16, "white_mistyrose", 255, 228, 225, "#ffe4e1"));
//yellow
List.push_back(ColorData(9, 0, "yellow_gold", 255, 215, 0, "#ffd700"));
List.push_back(ColorData(9, 1, "yellow_", 255, 255, 0, "#ffff00"));
List.push_back(ColorData(9, 2, "yellow_light", 255, 255, 224, "#ffffe0"));
List.push_back(ColorData(9, 3, "yellow_lemonchiffon", 255, 250, 205, "#fffacd"));
List.push_back(ColorData(9, 4, "yellow_lightgoldenrod", 250, 250, 210, "#fafad2"));
List.push_back(ColorData(9, 5, "yellow_papayawhip", 255, 239, 213, "#ffefd5"));
List.push_back(ColorData(9, 6, "yellow_moccasin", 255, 228, 181, "#ffe4b5"));
List.push_back(ColorData(9, 7, "yellow_peachpuff", 255, 218, 185, "#ffdab9"));
List.push_back(ColorData(9, 8, "yellow_palegoldenrod", 238, 232, 170, "#eee8aa"));
List.push_back(ColorData(9, 9, "yellow_khaki", 240, 230, 140, "#f0e68c"));
List.push_back(ColorData(9, 10, "yellow_darkkhaki", 189, 183, 107, "#bdb76b"));
};
void ColorFamily::FillColorData(ColorData &tar,ColorData* src)
{
tar.family = src->family;
tar.hex = src->hex;
tar.id = src->id;
tar.name = src->name;
tar.rgb[0] = src->rgb[0];
tar.rgb[1] = src->rgb[1];
tar.rgb[2] = src->rgb[2];
}
ColorData* ColorFamily::Get(const char * name)
{
for (int i = 0; i < List.size(); i++)
{
ColorData da = List[i];
if (strcmp(da.name, name))
{
_detail->family = da.family;
_detail->hex = da.hex;
_detail->id = da.id;
_detail->name = da.name;
_detail->rgb[0] = da.rgb[0];
_detail->rgb[1] = da.rgb[1];
_detail->rgb[2] = da.rgb[2];
return _detail;
}
}
}
ColorData* ColorFamily::Get(UINT8 _family, UINT16 _id)
{
for (int i = 0; i < List.size(); i++)
{
_detail = &List[i];
if (_detail->family == _family && _detail->id == _id)
{
return _detail;
}
}
}
bool ColorFamily::Get(UINT8 _r,UINT8 _g,UINT8 _b,ColorData &_out)
{
debug("rgb(%d,%d,%d)\n", _r,_g,_b);
struct similar
{
UINT8 dt;
UINT8 family;
UINT8 color;
similar(UINT8 _dt, UINT8 _family, UINT8 _color){
dt = _dt;
family = _family;
color = _color;
}
};
std::vector<similar> sList;
for (int i = 0; i < List.size(); i++)
{
_detail = &List[i];
if (_detail->rgb[0] == _r && _detail->rgb[1] == _g && _detail->rgb[2] == _b)
{
FillColorData(_out,_detail);
return true;
}
}
for (int i = 0; i < List.size(); i++)
{
_detail = &List[i];
unsigned short sum = 0;
if (Less10(_detail->rgb[0],_r) &&
Less10(_detail->rgb[1],_g) &&
Less10(_detail->rgb[2],_b))
{
sum += abs(_detail->rgb[0] - _r);
sum += abs(_detail->rgb[1] - _g);
sum += abs(_detail->rgb[2] - _b);
debug("dc:%d,list(%d,%d,%d)\n",sum, _detail->rgb[0],_detail->rgb[1],_detail->rgb[2]);
sList.push_back(similar(sum,_detail->family,_detail->id));
}
}
unsigned short dt_min_idx = 0;
if(sList.size() > 0)
{
for (int i = 0; i < sList.size(); i++)
{
if(sList[dt_min_idx].dt >= sList[i].dt)
{
dt_min_idx = i;
}
}
UINT8 fid = sList[dt_min_idx].family;
UINT8 cid = sList[dt_min_idx].color;
FillColorData(_out,Get(fid,cid));
return true;
}
return false;
}
}
//main.cpp
#include <iostream>
#include <windows.h>
#include "MyColor.h"
#include "debug.h"
using namespace std;
my::bug d;
my::ColorFamily col;
int main()
{
my::ColorData cd;
UINT8 rgb[3] = {62,138,120};
bool rst = col.Get(rgb[0],rgb[1],rgb[2],cd);
if(rst)
debug("rst:%d \n rgb(%d,%d,%d)=>rgb(%d,%d,%d) \nfamily:%d,color:%d,name:%s\n",rst,rgb[0],rgb[1],rgb[2],cd.rgb[0],cd.rgb[1],cd.rgb[2],cd.family,cd.id, cd.name);
else
debug("no result\n");
system("pause");
return 0;
}
//ctrl + shift + p
and the result is:
rgb(62,138,120)
dc:176,list(64,224,208)
dc:76,list(70,130,180)
dc:141,list(65,105,225)
dc:50,list(60,179,113)
rst:1
The result of the program is roughly in line with my expectations, but there is something very confusing about it.
Here, for example, I clearly specify that three conditions are required to be true at the same time before I add it to the list.
if (Less10(_detail->rgb[0],_r) &&
Less10(_detail->rgb[1],_g) &&
Less10(_detail->rgb[2],_b))
{
...
}
But judging from the output, it seems that the program only compares the first condition.
debug init done
rgb(62,138,120)
dc:176,list(64,224,208)
dc:76,list(70,130,180)
dc:141,list(65,105,225)
dc:50,list(60,179,113)
################################################
rst:1
rgb(62,138,120)=>rgb(60,179,113)
family:5,color:9,name:green_mediumsea
Your use of macros is causing confusion.
You defined Less10 as follows:
#define Less10(a,b) abs(a-b) < 10 ? true : false
So let us expand your condition:
abs(_detail->rgb[0]-_r) < 10 ? true : false &&
abs(_detail->rgb[1]-_g) < 10 ? true : false &&
abs(_detail->rgb[2]-_b) < 10 ? true : false)
Since : has a very low operator precedence, it is parsed as follows:
abs(_detail->rgb[0]-_r) < 10 ? true :
(false && abs(_detail->rgb[1]-_g) < 10 ? true :
(false && abs(_detail->rgb[2]-_b) < 10 ? true : false)))
and since false && EXPR === false, it boils down to:
abs(_detail->rgb[0]-_r) < 10 ? true : false
To fix it, either make Less10 a real function, or wrap the macro definition in parens, and get rid of the extraneous ? true : false while you're at it (since the condition is already true or false)
#define Less10(a,b) (abs(a-b) < 10)

Show an Icon inside Modal Dialog Box

I'm completely new to MFC and have been working on this issue for a couple of days now and can't find any solution that works.
Problem:
I have a dialog class (Modal Dialog Box) with a style defined in an .rc file (Code below) and get the resource id of the icon (int m_icon is the same as IDR_MAINFRAME) from another class (OtherClass.rc).
All the text information inside the Dialog Box is set dynamically (Code below) but the same doesn't work with the Icon. The marked Icon in the image below is what I'm trying to set.
The Icon Resource is defined in another .rc file and the LoadImage seems to work as I can set the small Icon in the top left of the window. The only problem is setting the big icon in this image. (Not shown at all, just an empty space)
OtherClass.rc
IDR_MAINFRAME ICON "res\\MyIcon.ico"
Dialog.rc
ABOUTBOX DIALOGEX 0, 0, 285, 77
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "<<Aboutbox>>"
FONT 8, "MS Shell Dlg", 0, 0, 0x0
BEGIN
ICON ABOUT_ICON, ABOUT_ICON, 11, 10, 21, 20
LTEXT "", IDC_STATIC, 40, 10, 163, 8, SS_NOPREFIX
LTEXT "<<Package Name 1.00>>", ABOUT_NAME, 40, 20, 163, 8, SS_NOPREFIX
LTEXT "<<FileName>>", ABOUT_FILENAME, 40, 30, 163, 8, SS_NOPREFIX
DEFPUSHBUTTON "OK", IDOK, 217, 7, 60, 14, WS_GROUP
END
Dialog.cpp
BOOL AboutDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Window Title
SetWindowText(L"About " + m_title);
// Set Icon
HICON hIcon = (HICON)LoadImage(GetModuleHandleW(NULL), MAKEINTRESOURCE(m_icon), IMAGE_ICON, 96, 96, LR_DEFAULTCOLOR);
SetIcon(hIcon, TRUE);
SetIcon(hIcon, FALSE);
// Text
SetDlgItemText(ABOUT_NAME, m_name);
SetDlgItemText(ABOUT_FILENAME, m_filename);
return TRUE;
}
What I've tried doing is:
1. GetDlgItem(ABOUT_ICON)->SetIcon(hIcon, TRUE);
2. SendMessage(WM_SETICON, ICON_BIG, (LPARAM)hIcon);
and many more things along those lines but the icon space just remains empty. Neither the LoadImage nor the GetDlgItem(ABOUT_ICON) returns a nullptr (already checked that).
You shouldn't have to do any thing like SetIcon or SendMessage to get an icon to show. What's wrong is your RC file is wrong. It should look like this:
ABOUTBOX DIALOGEX 0, 0, 285, 77
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "<<Aboutbox>>"
FONT 8, "MS Shell Dlg", 0, 0, 0x0
BEGIN
ICON ABOUT_ICON, IDC_STATIC, 11, 10, 21, 20
LTEXT "", IDC_STATIC, 40, 10, 163, 8, SS_NOPREFIX
LTEXT "<<Package Name 1.00>>", ABOUT_NAME, 40, 20, 163, 8, SS_NOPREFIX
LTEXT "<<FileName>>", ABOUT_FILENAME, 40, 30, 163, 8, SS_NOPREFIX
DEFPUSHBUTTON "OK", IDOK, 217, 7, 60, 14, WS_GROUP
END
Take a look at the ICON line after the BEGIN line.
You had it as ABOUT_ICON, ABOUT_ICON, 11, 10, 21, 20, but the 2nd parameter tells the framework what the ID of the control is. Since you want a Static control, you use the ID of IDC_STATIC which tells the framework there is no ID, but instead it should create a generic Static control.
If this doesn't fix it, I would investigate if ABOUT_ICON is wired up correctly, that it actually is pointing at an icon.
Also, why are you working in the RC file? MFC gives you a great GUI editor. For example, you could modify it using the UI:

windres fatal error: when writing output to : Invalid argument

I was trying to make windres compile a resource file from Visual Studio. I properly converted all the files (and the one included) to ANSI, but I still get this error every time I try to compile it :
windres.exe -J rc -O coff -i C:\Users\Gabriel\DOCUME~1\DoConfig\DoConfig\DoConfig.rc -o Debug\DoConfig.res
mingw32-g++.exe -o ..\Debug\DoConfig.exe Debug\DoConfig.o Debug\stdafx.o Debug\DoConfig.res -lwinmm.lib -lcomctl32.lib -lkernel32.lib -luser32.lib -lgdi32.lib -lwinspool.lib -lcomdlg32.lib -ladvapi32.lib -lshell32.lib -lole32.lib -loleaut32.lib -luuid.lib -lodbc32.lib -lodbccp32.lib -mwindows
C:\Users\Gabriel\DOCUME~1\DoConfig\DoConfig\DoConfig.rc:4:0: fatal error: when writing output to : Invalid argument
^
compilation terminated.
windres.exe: C:\\Users\\Gabriel\\DOCUME~1\\DoConfig\\DoConfig\\DoConfig.rc:22: syntax error
windres.exe: preprocessing failed.
Process terminated with status 1 (0 minute(s), 3 second(s))
3 error(s), 0 warning(s) (0 minute(s), 3 second(s))
Here is the start of the source code of the corresponding file :
#include "resource.h"
#include <windows.h>
IDI_DOCONFIG ICON "DoConfig.ico"
STRANGE_LAYOUT BITMAP "Strange_Layout.bmp"
NORMAL_LAYOUT BITMAP "Normal_Layout.bmp"
MENU_MAIN MENU
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
{
MENUITEM "Quit", 40001
MENUITEM "Note", 40002
}
DLG_CONFIG DIALOGEX 0, 0, 387, 281
STYLE DS_MODALFRAME | DS_CENTER | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Doukutsu Monogatari - Settings"
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
FONT 9, "Arial"
{
CONTROL "Use Gamepad", Button_Use_Gamepad, BUTTON, BS_AUTOCHECKBOX | WS_CHILD | WS_VISIBLE | WS_TABSTOP, 12, 16, 100, 12
CONTROL "", Button_Jump_1, BUTTON, BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE | WS_GROUP, 52, 48, 12, 12
CONTROL "", Button_Attack_1, BUTTON, BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE, 52, 64, 12, 12
CONTROL "", Button_Weapon_Plus_1, BUTTON, BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE, 52, 80, 12, 12
CONTROL "", Button_Weapon_Minus_1, BUTTON, BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE, 52, 96, 12, 12
CONTROL "", Button_Item_1, BUTTON, BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE, 52, 112, 12, 12
CONTROL "", Button_Map_1, BUTTON, BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE, 52, 128, 12, 12
CONTROL "", Button_Jump_2, BUTTON, BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE | WS_GROUP, 68, 48, 12, 12
CONTROL "", Button_Attack_2, BUTTON, BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE, 68, 64, 12, 12
CONTROL "", Button_Weapon_Plus_2, BUTTON, BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE, 68, 80, 12, 12
CONTROL "", Button_Weapon_Minus_2, BUTTON, BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE, 68, 96, 12, 12
CONTROL "", Button_Item_2, BUTTON, BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE, 68, 112, 12, 12
EDIT : Note that I looked at the .rc file with a hex editor and all the newlines are fine, the conversion to ANSI worked properly
EDIT 2 : The path to the .rc file doesn't have any spaces or anything that should screw with windres :
C:\Users\Gabriel\Documents\DoConfig\DoConfig\DoConfig.rc
EDIT 3 : The windows.h file is in my include directory, I checked manually. Plus, the .cpp file uses it and it didn't fail to compile.
EDIT 4 : Removing the line that includes the windows.h file doesn't change anything, it still gives the same error.
EDIT 5 : Naming every class by string instead of by name directly worked and eliminated all error messages.
Control's class name should be "BUTTON", not BUTTON
Button_Use_Gamepad should be defined as an integer
Corrected format:
CONTROL "Use Gamepad", 101, "BUTTON", BS_AUTOCHECKBOX | WS_TABSTOP, 12, 16, 100, 12

Directx error DrawTextA and LPCSTR and HDC

in Debug mode I get this:
Severity Code Description Project File Line
Error C2660 'DrawTextA': function does not take 4 arguments Win32Project6 c:\users\dani\documents\visual studio 2015\projects\win32project6\win32project6\hacks.cpp 41
Severity Code Description Project File Line
Error C2065 'i': undeclared identifier Win32Project6 c:\users\dani\documents\visual studio 2015\projects\win32project6\win32project6\hacks.cpp 53
Severity Code Description Project File Line
Error IntelliSense: argument of type "const char *" is incompatible with parameter of type "HDC" Win32Project6 c:\Users\Dani\Documents\Visual Studio 2015\Projects\Win32Project6\Win32Project6\Hacks.cpp 41
Severity Code Description Project File Line
Error IntelliSense: argument of type "int" is incompatible with parameter of type "LPCSTR" Win32Project6 c:\Users\Dani\Documents\Visual Studio 2015\Projects\Win32Project6\Win32Project6\Hacks.cpp 41
Severity Code Description Project File Line
Error IntelliSense: argument of type "D3DCOLOR" is incompatible with parameter of type "LPRECT" Win32Project6 c:\Users\Dani\Documents\Visual Studio 2015\Projects\Win32Project6\Win32Project6\Hacks.cpp 41
Severity Code Description Project File Line
Error IntelliSense: too few arguments in function call Win32Project6 c:\Users\Dani\Documents\Visual Studio 2015\Projects\Win32Project6\Win32Project6\Hacks.cpp 41
Severity Code Description Project File Line
Error C2065 'i': undeclared identifier Win32Project6 c:\users\dani\documents\visual studio 2015\projects\win32project6\win32project6\hacks.cpp 53
Severity Code Description Project File Line
Error C2228 left of '.name' must have class/struct/union Win32Project6 c:\users\dani\documents\visual studio 2015\projects\win32project6\win32project6\hacks.cpp 53
Severity Code Description Project File Line
Error C2228 left of '.c_str' must have class/struct/union Win32Project6 c:\users\dani\documents\visual studio 2015\projects\win32project6\win32project6\hacks.cpp 53
Severity Code Description Project File Line
Error C2660 'DrawTextA': function does not take 4 arguments Win32Project6 c:\users\dani\documents\visual studio 2015\projects\win32project6\win32project6\hacks.cpp 53
Severity Code Description Project File Line
Error IntelliSense: identifier "i" is undefined Win32Project6 c:\Users\Dani\Documents\Visual Studio 2015\Projects\Win32Project6\Win32Project6\Hacks.cpp 53
Severity Code Description Project File Line
Error IntelliSense: argument of type "int" is incompatible with parameter of type "LPCSTR" Win32Project6 c:\Users\Dani\Documents\Visual Studio 2015\Projects\Win32Project6\Win32Project6\Hacks.cpp 53
Severity Code Description Project File Line
Error IntelliSense: argument of type "D3DCOLOR" is incompatible with parameter of type "LPRECT" Win32Project6 c:\Users\Dani\Documents\Visual Studio 2015\Projects\Win32Project6\Win32Project6\Hacks.cpp 53
Severity Code Description Project File Line
Error IntelliSense: too few arguments in function call Win32Project6 c:\Users\Dani\Documents\Visual Studio 2015\Projects\Win32Project6\Win32Project6\Hacks.cpp 53
CODe for Hacks.cpp
#include "Hacks.h"
int MenuIndex;
D3DCOLOR fontRed = D3DCOLOR_ARGB(255, 255, 0, 0);
D3DCOLOR fontGreen = D3DCOLOR_ARGB(255, 0, 255, 0);
D3DCOLOR fontBlue = D3DCOLOR_ARGB(255, 0, 0, 255);
D3DCOLOR fontWhite = D3DCOLOR_ARGB(255, 255, 255, 255);
D3DCOLOR fontBlack = D3DCOLOR_ARGB(255, 0, 0, 0);
void Hacks::CreateFont(IDirect3DDevice9 *d3dDevice, std::string choiceFont)
{
D3DXCreateFont(d3dDevice, 20, 0, FW_BOLD, 0, FALSE, DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE,
choiceFont.c_str(), &Font);
}
void Hacks::InitializeMenuItems()
{
hack[WALLHACK].name = "WallHack and chams";
hack[CUSTOM_CROSSHAIR].name = "Show custom crosshair";
hack[NO_RECOIL].name = "No Recoil";
hack[UNLIM_AMMO].name = "Unlimited equipment";
hack[AUTO_FIRE].name = "Auto Fire";
hack[HIDE_MENU].name = "Hide menu [INSERT]";
hack[HIDE_MENU].on = false;
}
void Hacks::Draw_Text(LPCSTR TextToDraw, int x, int y, D3DCOLOR Color)
{
RECT rct = { x - 120, y + 120, y + 15 };
Font->DrawTextA(NULL, TextToDraw, -1, &rct, DT_NOCLIP, Color);
}
void Hacks::DrawMenu(IDirect3DDevice9 *d3dDevice)
{
if (!hack[HIDE_MENU].on)
{
DrawFilledRectangle(55, 20, 200, 50, fontBlue, d3dDevice);
DrawBorderBox(55, 20, 200, 50, 4, fontBlack, d3dDevice);
DrawTextA("GAME", 190, 30, fontWhite);
DrawFilledRectangle(30, 55, 250, (62 * MAX_MENU_ITEMS), fontBlue, d3dDevice);
DrawBorderBox(30, 55, 250, (62 * MAX_MENU_ITEMS), 6, fontBlack, d3dDevice);
int y = 40;
for (int i = 0; i < MAX_MENU_ITEMS; i++)
{
DrawFilledRectangle(45, 30 + y, 220, 40, hack[i].on ? fontGreen : fontRed, d3dDevice);
DrawBorderBox(45, 30 + y, 220, 40, 4, fontBlack, d3dDevice);
}
DrawTextA(hack[i].name.c_str(), 170, 39 + y, fontBlack);
y + 50;
}
}
void Hacks::DrawFilledRectangle(int x, int y, int width, int height, D3DCOLOR color, IDirect3DDevice9 *d3dDevice)
{
}
void Hacks::DrawBorderBox(int x, int y, int width, int height, int thickness, D3DCOLOR color, IDirect3DDevice9 *d3dDevice)
{
}
void Hacks::KeyboardInput()
{
}
CODE for Hacks.h
#include "d3d9.h"
#include <ctime>
#include <iostream>
#define D3DHOOK_TEXTURES
#define MAX_MENU_ITEMS 6
#define WALLHACK 0
#define CUSTOM_CROSSHAIR 1
#define NO_RECOIL 2
#define UNLIM_AMMO 3
#define AUTO_FIRE 4
#define HIDE_MENU 5
/*DEFINITION FOR OUT CHAMS*/
class Hacks
{
public:
int m_Stride;
void Hacks::CreateFont(IDirect3DDevice9 *d3dDevice, std::string choiceFont);
void Hacks::InitializeMenuItems();
void Hacks::Draw_Text(LPCSTR TextToDraw, int x, int y, D3DCOLOR Color);
void Hacks::DrawMenu(IDirect3DDevice9 *d3dDevice);
void Hacks::DrawFilledRectangle(int x, int y, int width, int height,D3DCOLOR color, IDirect3DDevice9 *d3dDevice);
void Hacks::DrawBorderBox(int x, int y, int width, int height, int thickness, D3DCOLOR color, IDirect3DDevice9 *d3dDevice);
void Hacks::KeyboardInput();
LPDIRECT3DTEXTURE9 texRed;
LPDIRECT3DTEXTURE9 textGreen;
LPDIRECT3DTEXTURE9 textBlue;
LPDIRECT3DTEXTURE9 textWhite;
D3DVIEWPORT9 ViewPort;
LPD3DXFONT Font;
struct d3dMenuHack
{
bool on;
std::string name;
};
d3dMenuHack hack[MAX_MENU_ITEMS];
};
YES, I have it set to multibyte characterset, and when I copied the code from the creator of the tutorial, it worked perfectly, but the thing is that they where the exact same code 100 % the same but one worked and the other did not, settings is set to win 32 and both debug and release gives errors.
this is tutorial code:
#include "hacks.h";
/*--------------CHEAT RELATED VARS-------------------*/
int MenuIndex = 0;
// Create a colour for the text
D3DCOLOR fontRed = D3DCOLOR_ARGB(255, 255, 0, 0);
D3DCOLOR fontGreen = D3DCOLOR_ARGB(255, 0, 255, 0);
D3DCOLOR fontBlue = D3DCOLOR_ARGB(255, 0, 42, 255);
D3DCOLOR fontWhite = D3DCOLOR_ARGB(255, 255, 255, 255);
D3DCOLOR fontBlack = D3DCOLOR_ARGB(255, 0, 0, 0);
/*---------------------------------------------------*/
void Hacks::CreateFont(IDirect3DDevice9 *d3dDevice, std::string choiceFont)
{
D3DXCreateFont( d3dDevice, 20, 0, FW_BOLD, 0, FALSE, DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE,
choiceFont.c_str(), &m_font );
}
void Hacks::InitializeMenuItems()
{
hack[WALLHACK].name = " WallHack and chams";
hack[CUSTOM_CROSSHAIR].name = " Show custom crosshair";
hack[NO_RECOIL].name = " No Recoil";
hack[UNLIM_AMMO].name = " Unlimited equipment";
hack[AUTO_FIRE].name = "All guns Automatic";
hack[HIDE_MENU].name = " Hide hack [INSERT]";
//make the hack visible by default
hack[HIDE_MENU].on = false; //shows hack by default
}
void Hacks::DrawMenu(IDirect3DDevice9 *d3dDevice)
{
if(!hack[HIDE_MENU].on)
{
//Add game name here, and put border around it
DrawFilledRect( 55, 20, 200, 50, fontBlue, d3dDevice );
DrawBorderBox(55, 20, 200, 50, 4, fontBlack, d3dDevice );
Draw_Text("COD 4 MP hack", 190, 30, fontWhite);
//draw back of our hack, transparent black
DrawFilledRect( 30, 55, 250, (62*MAX_MENU_ITEMS),fontBlue, d3dDevice );
//draw hack border
DrawBorderBox(30, 55, 250, (62*MAX_MENU_ITEMS), 6/*was 6*/, fontBlack, d3dDevice );
//Reset our time and update the text again in 2 secs
int y = 40;
for(int i = 0; i < MAX_MENU_ITEMS; i ++)
{
//Draw each box's back colour, this will be based on whether the hack is on e.g.
//red means off and green means on
DrawFilledRect( 45, 30+y, 220, 40, hack[i].on ? fontGreen : fontRed, d3dDevice );
//draw box Around Each hack item If the item is highlighted it will show with a white border
DrawBorderBox(45, 30+y, 220, 40, 4, fontBlack, d3dDevice );
//draw White border to show the user which hack item is currently selected
if(MenuIndex == i)
{
DrawBorderBox(41, 26+y, 228, 48, 4, fontWhite, d3dDevice );
}
//Ternary at its finest, if the hack is on we display the text colour in green
//otherwise its green
//Draw_Text(hack[i].KeyAssigned.c_str(), 160 , 32+y, fontWhite);
Draw_Text(hack[i].name.c_str(), 170 , 39+y, fontBlack);
//Draw_Text(hack[i].state. c_str(), 355 , 32+y, hack[i].on ? fontGreen : fontRed);
//used to position the next item below by 30 height units
y+= 50;
}
Draw_Text("Select using arrow keys", 170, ((62*MAX_MENU_ITEMS)+7), fontWhite);
Draw_Text("Turn ON/OFF [END] key", 170, ((62*MAX_MENU_ITEMS)+27), fontWhite);
}
}
void Hacks::DrawBorderBox( int x, int y, int w, int h, int thickness, D3DCOLOR Colour, IDirect3DDevice9 *pDevice)
{
//Top horiz line
DrawFilledRect( x, y, w, thickness, Colour, pDevice );
//Left vertical line
DrawFilledRect( x, y, thickness, h, Colour, pDevice );
//right vertical line
DrawFilledRect( (x + w), y, thickness, h, Colour, pDevice );
//bottom horiz line
DrawFilledRect( x, y + h, w+thickness, thickness, Colour, pDevice );
}
//We receive the 2-D Coordinates the colour and the device we want to use to draw those colours with
void Hacks::DrawFilledRect(int x, int y, int w, int h, D3DCOLOR color, IDirect3DDevice9* dev)
{
//We create our rectangle to draw on screen
D3DRECT BarRect = { x, y, x + w, y + h };
//We clear that portion of the screen and display our rectangle
dev->Clear(1, &BarRect, D3DCLEAR_TARGET | D3DCLEAR_TARGET, color, 0, 0);
}
void Hacks::Draw_Text(LPCSTR TextToDraw, int x, int y, D3DCOLOR Colour)
{
// Create a rectangle to indicate where on the screen it should be drawn
RECT rct = {x- 120, y, x+ 120, y + 15};
// Draw some text
m_font->DrawText(NULL, TextToDraw, -1, &rct, DT_NOCLIP, Colour );
}
void Hacks::KeyboardInput()
{
if(GetAsyncKeyState(VK_UP)&1)
{
if(MenuIndex > 0)
{
MenuIndex--;
}
}
if(GetAsyncKeyState(VK_DOWN)&1)
{
if(MenuIndex < MAX_MENU_ITEMS-1)
{
MenuIndex++;
}
}
if(GetAsyncKeyState(VK_END)&1)
{
hack[MenuIndex].on = !hack[MenuIndex].on;
if(MenuIndex == NO_RECOIL)
{
//this is where we write memory, these are nop values
}
if(MenuIndex == UNLIM_AMMO)
{
//this is where we write memory
}
}
if(GetAsyncKeyState(VK_INSERT)&1)
{
//TEXT DOESNT CHANGE as the hack is either hidden or not
//and if its hidden you cant tell the user to turn hack on(at least we wont)
hack[HIDE_MENU].on = !hack[HIDE_MENU].on;
}
}
Whati s weird about this is that in the video he uses DrawTextA, youtube; fleep Hacks.
but if I use Draw_Text it also gives error
DrawTextA takes 5 arguments, and you're only passing in 4.
INT DrawText(
[in] LPD3DXSPRITE pSprite,
[in] LPCTSTR pString,
[in] INT Count,
[in] LPRECT pRect,
[in] DWORD Format,
[in] D3DCOLOR Color
);
The pSprite variable can be NULL, if you view the source code you're pasting he puts NULL as the first argument in every function call. Just put a NULL in front of your other 4 arguments and it will compile.

Win32: Storing Multi-Line Text in a Buffer

How would I go about displaying multi-line text in Win32? This code is within my GamePaint() function, and I want to write the top 5 High Scores (stored in an attribute of a struct) out to the screen. I can get it to successfully output a single line using this method...how do I make the TCHAR buffer, szText, store multiple lines? Here's what I've attempted so far:
Original Code:
//draw rect for normal scores
ChangeTextFormat(hDC, hWnd, 1);
TCHAR szText[64];
RECT rcNormalScores = { 268, 122, 436, 330};
RECT rcHardScores = { 37, 122, 198, 330};
//stringstream ssTemp;
for(int i = 0; i < 5; i++)
{
//ssTemp << i;
//display nth Normal score
wsprintf(szText, "%d \n", g_scoreTop[i].num);
DrawText(hDC, szText, -1, &rcNormalScores, DT_LEFT | DT_WORDBREAK);
}
EDIT: Thanks for the info, but I'm still having some difficulty converting between data types. Here's the error I'm getting:
cannot convert from 'std::basic_string<_Elem,_Traits,_Ax>' to 'std::basic_string<_Elem,_Traits,_Ax>'
EDIT2: Thanks for the help, queen3. I've posted the working code below:
Working Code:
ChangeTextFormat(hDC, hWnd, 1);
RECT rcNormalScores = { 37, 122, 198, 330};
RECT rcHardScores = { 268, 122, 436, 330};
stringstream ssTemp;
for(int i = 0; i < 5; i++)
{
ssTemp << g_scoreTop[i].num << " \n";
}
string sTemp = ssTemp.str();
LPCSTR LPTemp = (LPCSTR)sTemp.c_str();
DrawText(hDC, LPTemp, -1, &rcNormalScores, DT_LEFT | DT_WORDBREAK);
DrawText(hDC, LPTemp, -1, &rcHardScores, DT_LEFT | DT_WORDBREAK);
Either of
Make one string with all lines and newlines and do single DrawText
Adjust rcNormalScores .top each time by adding height of the string (for this you can use DT_CALCRECT flag)
The first one might work better if you later decide to change DT_LEFT to DT_CENTER.