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.
Related
I am trying to migrate my graphics interface project from Gdiplus to Direct2D.
Currently, I have a code that calculates clipping area for an rendering object:
Graphics g(hdc);
Region regC = Rect(x, y, cx + padding[2] + padding[0], cy + padding[3] + padding[1]);
RecursRegPos(this->parent, ®C);
RecursRegClip(this->parent, ®C);
g.setClip(g);
...
inline void RecursRegClip(Object *parent, Region* reg)
{
if (parent == CARD_PARENT)
return;
if (parent->overflow != OVISIBLE)
{
Rect regC(parent->getAbsoluteX(), parent->getAbsoluteY(), parent->getCx(), parent->getCy()); // Implementation of these function is not necceassary
GraphicsPath path;
path.Reset();
GetRoundRectPath(&path, regC, parent->borderRadius[0]);
// source https://stackoverflow.com/a/71431813/15220214, but if diameter is zero, just plain rect is returned
reg->Intersect(&path);
}
RecursRegClip(parent->parent, reg);
}
inline void RecursRegPos(Object* parent, Rect* reg)
{
if (parent == CARD_PARENT)
return;
reg->X += parent->getX() + parent->padding[0];
reg->Y += parent->getY() + parent->padding[1];
if (parent->overflow == OSCROLL || parent->overflow == OSCROLLH)
{
reg->X -= parent->scrollLeft;
reg->Y -= parent->scrollTop;
}
RecursRegPos(parent->parent, reg);
}
And now I need to convert it to Direct2D. As You may notice, there is no need to create Graphics object to get complete calculated clipping region, so I it would be cool if there is way to just convert Region to ID2D1Geometry*, that, as far, as I understand from msdn article need to create clipping layer.
Also, there is probably way to convert existing code (RecursRegClip, RecursRegPos) to Direct2D, but I am facing problems, because I need to work with path, but current functions get region as an argument.
Update 1
There is Region::GetData method that returns, as I understand array of points, so maybe there is possibility to define either ID2D1PathGeometry or ID2D1GeometrySink by points?
Update 2
Oh, maybe
ID2D1GeometrySink::AddLines(const D2D1_POINT_2F *points, UINT32 pointsCount)
is what do I need?
Unfortunately, GetData of region based on just (0,0,4,4) rectangle returns 36 mystique values:
Region reg(Rect(0, 0, 4, 4));
auto so = reg.GetDataSize();
BYTE* are = new BYTE[so];
UINT fi = 0;
reg.GetData(are, so, &fi);
wchar_t ou[1024]=L"\0";
for (int i = 0; i < fi; i++)
{
wchar_t buf[10] = L"";
_itow_s(are[i], buf, 10, 10);
wcscat_s(ou, 1024, buf);
wcscat_s(ou, 1024, L", ");
}
// ou - 28, 0, 0, 0, 188, 90, 187, 128, 2, 16, 192, 219, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 64, 0, 0, 128, 64,
I rewrote the solution completely, it seems to be working:
// zclip is ID2D1PathGeometry*
inline void Render(ID2D1HwndRenderTarget *target)
{
ID2D1RoundedRectangleGeometry* mask = nullptr;
ID2D1Layer* clip = nullptr;
if(ONE_OF_PARENTS_CLIPS_THIS || THIS_HAS_BORDER_RADIUS)
{
Region reg = Rect(x, y, cx + padding[2] + padding[0], cy + padding[3] + padding[1]);
RecursRegPos(this->parent, ®);
D2D1_ROUNDED_RECT maskRect;
maskRect.rect.left = reg.X;
maskRect.rect.top = reg.Y;
maskRect.rect.right = reg.X + reg.Width;
maskRect.rect.bottom = reg.Y + reg.Height;
maskRect.radiusX = this->borderRadius[0];
maskRect.radiusY = this->borderRadius[1];
factory->CreateRoundedRectangleGeometry(maskRect, &mask);
RecursGeoClip(this->parent, mask);
target->CreateLayer(NULL, &clip);
if(zclip)
target->PushLayer(D2D1::LayerParameters(D2D1::InfiniteRect(), zclip), clip);
else
target->PushLayer(D2D1::LayerParameters(D2D1::InfiniteRect(), mask), clip);
SafeRelease(&mask);
}
// Draw stuff here
if (clip)
{
target->PopLayer();
SafeRelease(&clip);
SafeRelease(&mask);
SafeRelease(&zclip);
}
}
...
inline void RecursGeoClip(Object* parent, ID2D1Geometry* geo)
{
if (parent == CARD_PARENT)
return;
ID2D1RoundedRectangleGeometry* maskParent = nullptr;
if (parent->overflow != OVISIBLE)
{
Rect regC(parent->getAbsoluteX(), parent->getAbsoluteY(), parent->getCx(), parent->getCy());
ID2D1GeometrySink* sink = nullptr;
ID2D1PathGeometry* path = nullptr;
SafeRelease(&path);
factory->CreatePathGeometry(&path);
D2D1_ROUNDED_RECT maskRect;
maskRect.rect.left = regC.X;
maskRect.rect.top = regC.Y;
maskRect.rect.right = regC.X + regC.Width;
maskRect.rect.bottom = regC.Y + regC.Height;
maskRect.radiusX = parent->borderRadius[0];
maskRect.radiusY = parent->borderRadius[1];
path->Open(&sink);
factory->CreateRoundedRectangleGeometry(maskRect, &maskParent);
geo->CombineWithGeometry(maskParent, D2D1_COMBINE_MODE_INTERSECT, NULL, sink);
sink->Close();
SafeRelease(&sink);
SafeRelease(&this->zclip);
this->zclip = path;
RecursGeoClip(parent->parent, this->zclip);
}
else
RecursGeoClip(parent->parent, geo);
SafeRelease(&maskParent);
}
Now I can enjoy drawing one image and two rectangles in more than 60 fps, instead of 27 (in case of 200x200 image size, higher size - lower fps) with Gdi+ -_- :
I´m starting to use allegro 4.4.2 on Visual Studio 2013. I installed both allegro 4.4.2 and 5.0.10 on VS and started testing some examples of allegro 4.4.2
This is my code:
#include <allegro.h>
#define ANCHO 640
#define ALTO 480
int soltado = 1;
int accion = 4;
BITMAP *buffer;
BITMAP *dibujo;
BITMAP *botones;
bool Sobre_boton(){
return (mouse_x >0 && mouse_x < 64 &&
mouse_y >0 && mouse_y < 64);
};
void cambiaccion(){};
void realizaccion(){};
void Boton_izquierdo(){
if (Sobre_boton()){
cambiaccion();
}
else{
realizaccion();
}
};
void Pinta_cursor(){
circle(buffer, mouse_x, mouse_y, 2, 0x000000);
putpixel(buffer, mouse_x, mouse_y, 0x000000);
};
void Pinta_botones(){
blit(botones, buffer, 0, 0, 0, 0, 64, 64);
};
int main()
{
allegro_init();
install_keyboard();
install_mouse();
set_color_depth(32);
set_gfx_mode(GFX_AUTODETECT_WINDOWED, ANCHO, ALTO, 0, 0);
buffer = create_bitmap(ANCHO, ALTO);
dibujo = create_bitmap(ANCHO, ALTO);
botones = load_bmp("bton.bmp", NULL);
clear_to_color(buffer, 0xFFFFFF);
clear_to_color(dibujo, 0xFFFFFF);
while (!key[KEY_ESC]){
blit(dibujo, buffer, 0, 0, 0, 0, ANCHO, ALTO);
Pinta_botones();
//pulsa boton izquierdo
if (mouse_b & 1){
Boton_izquierdo();
}
else{
soltado = 1;
}
Pinta_cursor();
blit(buffer, screen, 0, 0, 0, 0, ANCHO, ALTO);
}
destroy_bitmap(botones);
destroy_bitmap(dibujo);
destroy_bitmap(buffer);
return 0;
}
END_OF_MAIN();
When I run the project, VS starts lagging horribly, to the point I have to wait like 7 seconds to see my mouse cursor move. I have to terminate the process of VS in order to get my pc to work normally again. Here´s a screenshot of the exception:
Can anyone tell what I´m doing wrong?
Thank you
In this part botones = load_bmp("bton.bmp", NULL); you should add something after, like:
if( botones == NULL )
return 0;
To validate whether it was loaded properly or not, as load_bmp will return a NULL pointer if it fails to correctly load the file. When Pinta_botones is called, the function blit is called, whose functionality is to copy a rectangular area of the source bitmap to the destination bitmap.
The source bitmap, in this case botones appears to be a NULL pointer in the screenshot when blit is called, which will cause problems when trying to access a NULL reference.
Ok so just kind of want to know a trick to get around this.
So i want the text "window will close in 10 seconds" to erase every time it goes through the loop and replace with the next number counting down. But all i get now is overlapping.
I just want it to count down and display as it goes.
//FILE: Main.cpp
//PROGR: Hank Bates
//PURPOSE: To display text on screen for 10 seconds.
//EXTRA ADD ON FILES: Slendermanswriting.ttf
// PrometheusSiren.wav
#include <allegro5\allegro.h>
#include <allegro5\allegro_font.h>
#include <allegro5\allegro_ttf.h>
#include <allegro5\allegro_native_dialog.h>
#include <allegro5\allegro_audio.h>
#include <allegro5\allegro_acodec.h>
int main(void)
{
//summon the fonts and stuff
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_FONT *font50;
ALLEGRO_FONT *font36;
ALLEGRO_FONT *font18;
ALLEGRO_SAMPLE *song;
int a = 100;
if (!al_init())
{
al_show_native_message_box(NULL, NULL, NULL,
"failed to initialize allegro!", NULL, NULL);
return -1;
}
//set up some of the display settings
al_set_new_display_flags(ALLEGRO_WINDOWED | ALLEGRO_RESIZABLE);
display = al_create_display(640, 480);
al_set_window_title(display, "A bad horror game");
if (!display)
{
al_show_native_message_box(NULL, NULL, NULL,
"failed to initialize display!", NULL, NULL);
return -1;
}
al_init_font_addon();
al_init_ttf_addon();
//Install Slender Man font here
font50 = al_load_font("Slendermanswriting.ttf", 50, 0);
font36 = al_load_font("Slendermanswriting.ttf", 36, 0);
font18 = al_load_font("Slendermanswriting.ttf", 18, 0);
//set up music here
al_install_audio();
al_init_acodec_addon();
al_reserve_samples(1);
song = al_load_sample("PrometheusSiren.wav");
//play song this will loop around and around like a record man!
al_play_sample(song, 1, 0, 1, ALLEGRO_PLAYMODE_LOOP, NULL);
int screen_w = al_get_display_width(display);
int screen_h = al_get_display_height(display);
al_clear_to_color(al_map_rgb(0, 0, 0));
al_draw_text(font50, al_map_rgb(255, 0, 0), 12, 50, 0, "SLENDER MAN IS COMING");
al_draw_text(font36, al_map_rgb(255, 5, 10), 200, 100, 0, "RUN AND HIDE");
al_draw_text(font18, al_map_rgb(100, 15, 18), 150, 150, 0, "ENJOY THE PROMETHEUS SIREN MUSIC");
int timer = 10;
while (timer != 0)
{
al_draw_textf(font18, al_map_rgb(255, 255, 255), screen_w / 3, 300, ALLEGRO_ALIGN_CENTRE,
"TURN UP YOUR VOLUME TO %i PRECENT!", a);
al_draw_textf(font18, al_map_rgb(255, 255, 255), screen_w / 2, 400, ALLEGRO_ALIGN_CENTRE,
"WINDOW WILL CLOSE IN %i seconds!", timer);
al_flip_display();
al_rest(1.0);
timer = timer - 1;
}
al_rest(10.0);
//destroy stuff
al_destroy_font(font18);
al_destroy_font(font50);
al_destroy_font(font36);
al_destroy_display(display);
al_destroy_sample(song);
//pew pew pew, bang.... all destoryed :)
return 0;
}
Move the background clear and first three text outputs into the main loop. You need to redraw everything on each frame.
//FILE: Main.cpp
//PROGR: Hank Bates
//PURPOSE: To display text on screen for 10 seconds.
//EXTRA ADD ON FILES: Slendermanswriting.ttf
// PrometheusSiren.wav
#include <allegro5\allegro.h>
#include <allegro5\allegro_font.h>
#include <allegro5\allegro_ttf.h>
#include <allegro5\allegro_native_dialog.h>
#include <allegro5\allegro_audio.h>
#include <allegro5\allegro_acodec.h>
int main(void)
{
//summon the fonts and stuff
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_FONT *font50;
ALLEGRO_FONT *font36;
ALLEGRO_FONT *font18;
ALLEGRO_SAMPLE *song;
int a = 100;
int time_left = 10;
//test redaw
ALLEGRO_EVENT_QUEUE *event_queue = NULL;
ALLEGRO_TIMER *timer = NULL;
bool redraw = true;
if (!al_init())
{
al_show_native_message_box(NULL, NULL, NULL,
"failed to initialize allegro!", NULL, NULL);
return -1;
}
//set up some of the display settings
al_set_new_display_flags(ALLEGRO_WINDOWED | ALLEGRO_RESIZABLE);
display = al_create_display(640, 480);
al_set_window_title(display, "A bad horror game");
if (!display)
{
al_show_native_message_box(NULL, NULL, NULL,
"failed to initialize display!", NULL, NULL);
return -1;
}
al_init_font_addon();
al_init_ttf_addon();
//Install Slender Man font here
font50 = al_load_font("Slendermanswriting.ttf", 50, 0);
font36 = al_load_font("Slendermanswriting.ttf", 36, 0);
font18 = al_load_font("Slendermanswriting.ttf", 18, 0);
//set up music here
al_install_audio();
al_init_acodec_addon();
al_reserve_samples(1);
//upload the song here
song = al_load_sample("PrometheusSiren.wav");
//play song this will loop around and around like a record man!
al_play_sample(song, 1, 0, 1, ALLEGRO_PLAYMODE_LOOP, NULL);
int screen_w = al_get_display_width(display);
int screen_h = al_get_display_height(display);
al_clear_to_color(al_map_rgb(0, 0, 0));
while (time_left != 0)
{
al_flip_display();
al_clear_to_color(al_map_rgb(0, 0, 0));
al_draw_text(font50, al_map_rgb(255, 0, 0), 12, 50, 0, "SLENDER MAN IS COMING");
al_draw_text(font36, al_map_rgb(255, 5, 10), 200, 100, 0, "RUN AND HIDE");
al_draw_text(font18, al_map_rgb(100, 15, 18), 150, 150, 0, "ENJOY THE PROMETHEUS SIREN MUSIC");
al_draw_textf(font18, al_map_rgb(255, 255, 255), screen_w / 3, 300, ALLEGRO_ALIGN_CENTRE,
"TURN UP YOUR VOLUME TO %i PRECENT!", a);
al_draw_textf(font18, al_map_rgb(255, 255, 255), screen_w / 2, 400, ALLEGRO_ALIGN_CENTRE,
"WINDOW WILL CLOSE IN %i seconds!", time_left);
al_rest(1.0);
time_left = time_left - 1;
}
al_flip_display();
al_rest(0.0);
//destroy stuff
al_destroy_font(font18);
al_destroy_font(font50);
al_destroy_font(font36);
al_destroy_display(display);
al_destroy_sample(song);
al_destroy_timer(timer);
//pew pew pew, bang.... all destoryed :)
return 0;
}
Yo i got it. Thanks for the point in the right question.
I am using a string table in my project and I want to store RGB values in it. how do I convert rgb value from LoadString char to something that can be used for COLORREF for CreateSolidBrush.
According to MSDN COLORREF is a DWORD typedef. So COLORREF cRef = RGB( 0, 0, 0 );is also valid. What I've understood you're trying to is something like:
const char *szTable[] = { "RGB( 255, 255, 255)", "RGB( 255, 0, 255)" }; //etc
COLORREF dwMyColor = szTable[0];
Getting a COLORREF object from a string table. I'm my opinion this is what you should do instead of string table:
COLORREF dwColor1 = RGB( 255, 0, 255 );
COLORREF dwTable[] = { 0xff00ff00, 0xffffffff, dwColor1 };
Why do you NEED a string table to store RGB values anyway? It wasn't clear enough I guess
I got it figured out using:
LoadString(g_hInstance,IDS_STRING151,rBuffer,256);
LoadString(g_hInstance,IDS_STRING152,gBuffer,256);
LoadString(g_hInstance,IDS_STRING153,bBuffer,256);
int r,g,b;
if(EOF == sscanf_s(rBuffer, "%d", &r))
{
//error
}
if(EOF == sscanf_s(gBuffer, "%d", &g))
{
//error
}
if(EOF == sscanf_s(bBuffer, "%d", &b))
{
//error
}
I have this:
void showNumbers(){
nrBtn1 = TTF_RenderText_Blended( fontnrs, "1", sdlcolors[0] );
nrBtn2 = TTF_RenderText_Blended( fontnrs, "2", sdlcolors[1] );
nrBtn3 = TTF_RenderText_Blended( fontnrs, "3", sdlcolors[2] );
nrBtn4 = TTF_RenderText_Blended( fontnrs, "4", sdlcolors[3] );
nrBtn5 = TTF_RenderText_Blended( fontnrs, "5", sdlcolors[4] );
nrBtn6 = TTF_RenderText_Blended( fontnrs, "6", sdlcolors[5] );
nrBtn7 = TTF_RenderText_Blended( fontnrs, "7", sdlcolors[6] );
nrBtn8 = TTF_RenderText_Blended( fontnrs, "8", sdlcolors[7] );
nrBtn9 = TTF_RenderText_Blended( fontnrs, "9", sdlcolors[8] );
SDL_Rect rcnrBtn1 = { 40, 32, 0, 0 };
SDL_Rect rcnrBtn2 = { 70, 32, 0, 0 };
SDL_Rect rcnrBtn3 = { 100, 32, 0, 0 };
SDL_Rect rcnrBtn4 = { 130, 32, 0, 0 };
SDL_Rect rcnrBtn5 = { 160, 32, 0, 0 };
SDL_Rect rcnrBtn6 = { 190, 32, 0, 0 };
SDL_Rect rcnrBtn7 = { 220, 32, 0, 0 };
SDL_Rect rcnrBtn8 = { 250, 32, 0, 0 };
SDL_Rect rcnrBtn9 = { 280, 32, 0, 0 };
SDL_BlitSurface(nrBtn1, NULL, screen, &rcnrBtn1); SDL_FreeSurface(nrBtn1);
SDL_BlitSurface(nrBtn2, NULL, screen, &rcnrBtn2); SDL_FreeSurface(nrBtn2);
SDL_BlitSurface(nrBtn3, NULL, screen, &rcnrBtn3); SDL_FreeSurface(nrBtn3);
SDL_BlitSurface(nrBtn4, NULL, screen, &rcnrBtn4); SDL_FreeSurface(nrBtn4);
SDL_BlitSurface(nrBtn5, NULL, screen, &rcnrBtn5); SDL_FreeSurface(nrBtn5);
SDL_BlitSurface(nrBtn6, NULL, screen, &rcnrBtn6); SDL_FreeSurface(nrBtn6);
SDL_BlitSurface(nrBtn7, NULL, screen, &rcnrBtn7); SDL_FreeSurface(nrBtn7);
SDL_BlitSurface(nrBtn8, NULL, screen, &rcnrBtn8); SDL_FreeSurface(nrBtn8);
SDL_BlitSurface(nrBtn9, NULL, screen, &rcnrBtn9); SDL_FreeSurface(nrBtn9);
}
But for 60 buttons. Is there a way how to do something like:
void showNumbers()
{
SDL_Rect rcnrBtn1 = { 40, 32, 0, 0 };
SDL_Rect rcnrBtn2 = { 70, 32, 0, 0 };
SDL_Rect rcnrBtn3 = { 100, 32, 0, 0 };
SDL_Rect rcnrBtn4 = { 130, 32, 0, 0 };
SDL_Rect rcnrBtn5 = { 160, 32, 0, 0 };
SDL_Rect rcnrBtn6 = { 190, 32, 0, 0 };
SDL_Rect rcnrBtn7 = { 220, 32, 0, 0 };
SDL_Rect rcnrBtn8 = { 250, 32, 0, 0 };
SDL_Rect rcnrBtn9 = { 280, 32, 0, 0 };
for(int x=1; x<=60;x++){
nrBtn+x = TTF_RenderText_Blended( fontnrs, x, sdlcolors[x-1] );
SDL_BlitSurface(nrBtn+x, NULL, screen, &rcnrBtn+x); SDL_FreeSurface(nrBtn+x);
}
}
You need to use an array.
E.g.
SDL_Rect rcnrBtn[60];
for(int x = 0; x < 60; x++) {
rcnrBtn[x].x = 30 * x + 10;
rcnrBtn[x].y = 32;
rcnrBtn[x].w = 100;
rcnrBtn[x].h = 24;
}
Arrays always start at 0, and this particular one ends at 59 giving a total of 60 elements.
You could do something like this :
void showNumbers() {
SDL_Surface* nrBtn = NULL;
int nbr = 1;
int x = 30; // your starting x
int i = 0;
for (; i < 60; ++i) {
nrBtn = TTF_RenderText_Blended(fontnrs, nbr_to_str(nbr), sdlcolors[i]); // You'll have to code nbr_to_str
SDL_Rect rect = {x, 32, 0, 0}; // Are you sure that width and height are 0 ?
SDL_BlitSurface(nrBtn, NULL, screen, &rect);
SDL_FreeSurface(nrBtn);
x += 30;
}
return;
}
It appears your entire function could be replaced with the following array-based variation. Assuming that your nrBtnXX variables are defined outside the function and you want to minimise the scope of changes, you should look at something like:
#define BUTTON_COUNT 60
SDL_Surface *nrBtn[BUTTON_COUNT];
void showNumbers () {
char textNum[3];
for (int i = 0; i < BUTTON_COUNT; i++) {
sprintf (textNum, "%d", i);
nrBtn[i] = TTF_RenderText_Blended( fontnrs, textNum, sdlcolors[i] );
}
SDL_Rect rcnrBtn[BUTTON_COUNT];
for (int i = 0; i < BUTTON_COUNT; i++) {
rcnrBtn[i].x = 40 + i * 30; // use formulae for all these.
rcnrBtn[i].y = 32;
rcnrBtn[i].w = 0;
rcnrBtn[i].h = 0;
}
for (int i = 0; i < BUTTON_COUNT; i++) {
SDL_BlitSurface(nrBtn[i], NULL, screen, &rcnrBtn[i]);
SDL_FreeSurface(nrBtn[i]);
}
}
The idea is to store everything in arrays so that you don't have to deal with individual variables. If the nrBtn variables are required to be a non-array, then I would set up a single array of pointers to them so this approach would still work, something like:
SDL_Surface *nrBtnPtr[] = { &nrBtn1, &nrBtn2 ..., &nrBtn60 };
You should also set the formulae intelligently for the x and y coordinates since you probably don't want a 60x1 matrix for them. Look into making it 12x5 or something equally as compact.