Want some text to erase its self in game - c++

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.

Related

Allegro5 Problems with deleting a text and display a new one after pressing a button

So I wrote this code where if I press enter it should clear the page and then enter a new text that says "game starts". but it won't run at all and just stays the same, anyone knows how to fix this?
PS. Extra question, how do I create a delay after replacing the text for 5 seconds then clear the text again?
Thank!
#include <iostream>
#include<allegro5/allegro.h>
#include<allegro5/allegro_ttf.h>
#include<allegro5/allegro_font.h>
#include<time.h>
#include<stdlib.h>
#include<stdio.h>
#include<Windows.h>
int main()
{
al_init();
al_init_font_addon();
al_init_ttf_addon();
ALLEGRO_DISPLAY* display = al_create_display(640, 480);
ALLEGRO_FONT* font = al_load_ttf_font("YARDSALE.ttf", 30, 0);
ALLEGRO_EVENT_QUEUE* queue = al_create_event_queue();
al_install_keyboard();
al_install_mouse();
al_register_event_source(queue, al_get_keyboard_event_source());
al_register_event_source(queue, al_get_mouse_event_source());
bool done = false;
while (!done) {
al_clear_to_color(al_map_rgb(255, 255, 255));
al_draw_text(font, al_map_rgb(139, 0, 0), 320, 150, ALLEGRO_ALIGN_CENTER, "Press Enter to start");
al_draw_text(font, al_map_rgb(148, 0, 211), 320, 300, ALLEGRO_ALIGN_CENTER, "PRESS ESC TO QUIT");
al_flip_display();
ALLEGRO_EVENT event;
al_wait_for_event(queue, &event);
if (event.type == ALLEGRO_EVENT_KEY_UP)
{
switch (event.keyboard.keycode)
{
case ALLEGRO_KEY_ESCAPE:
done = true;
break;
case ALLEGRO_KEY_ENTER:
al_clear_to_color(al_map_rgb(255, 255, 255));
al_draw_text(font, al_map_rgb(0, 255, 0), 300, 200, 0, "Game Starts");
al_flip_display;
break;
}
}
}
al_destroy_font(font);
al_destroy_display(display);
}
You simply missed "()" when you called 'al_flip_display'. It should be 'al_flip_display();'.
But even with this it will not work as expected, because "Game starts" will only appears for a moment. I suggest adding game states.
And I think at the end of the main function you should also destroy event queue.
#include<allegro5/allegro.h>
#include<allegro5/allegro_ttf.h>
#include<allegro5/allegro_font.h>
#include<time.h>
#include<stdlib.h>
#include<stdio.h>
#include<Windows.h>
enum class GAME_STATE { CLICK, INTRO, MAIN };
int main()
{
al_init();
al_init_font_addon();
al_init_ttf_addon();
ALLEGRO_DISPLAY* display = al_create_display(640, 480);
ALLEGRO_FONT* font = al_load_ttf_font("font_code_pro.ttf", 30, 0);
ALLEGRO_EVENT_QUEUE* queue = al_create_event_queue();
ALLEGRO_TIMER* timer = al_create_timer(1 / 60.0);
al_install_keyboard();
al_install_mouse();
al_register_event_source(queue, al_get_keyboard_event_source());
al_register_event_source(queue, al_get_mouse_event_source());
al_register_event_source(queue, al_get_timer_event_source(timer));
bool done = false;
bool draw = false;
unsigned delay = 0;
GAME_STATE state = GAME_STATE::CLICK;
al_start_timer(timer);
while(!done)
{
ALLEGRO_EVENT event;
al_wait_for_event(queue, &event);
if(event.type == ALLEGRO_EVENT_TIMER)
{
draw = true;
if(state == GAME_STATE::INTRO && !((++delay) % (60 * 5)))state = GAME_STATE::MAIN;
}
if(event.type == ALLEGRO_EVENT_KEY_DOWN)
{
switch(event.keyboard.keycode)
{
case ALLEGRO_KEY_ESCAPE:
done = true;
break;
case ALLEGRO_KEY_ENTER:
if(state == GAME_STATE::CLICK)
state = GAME_STATE::INTRO;
break;
}
}
if(draw)
{
draw = false;
switch(state)
{
case GAME_STATE::CLICK:
al_clear_to_color(al_map_rgb(255, 255, 255));
al_draw_text(font, al_map_rgb(139, 0, 0), 320, 150, ALLEGRO_ALIGN_CENTER, "Press Enter to start");
al_draw_text(font, al_map_rgb(148, 0, 211), 320, 300, ALLEGRO_ALIGN_CENTER, "PRESS ESC TO QUIT");
al_flip_display();
break;
case GAME_STATE::INTRO:
al_clear_to_color(al_map_rgb(255, 255, 255));
al_draw_text(font, al_map_rgb(0, 255, 0), 300, 200, 0, "Game Starts");
al_flip_display();
break;
case GAME_STATE::MAIN:
al_clear_to_color(al_map_rgb(255, 255, 255));
//
al_flip_display();
break;
default:
break;
}
}
}
al_destroy_font(font);
al_destroy_timer(timer);
al_destroy_event_queue(queue);
al_destroy_display(display);
}
I hope it helps! :)

How to make text blinking without blocking the whole program in C++ Allegro?

bool running = true;
int width = al_get_display_width(display);
while (running) {
for (;;) {
al_clear_to_color(al_map_rgb(255, 255, 255));
al_draw_bitmap(bitmap, 0, 0, 0);
al_draw_text(font, al_map_rgb(0, 0, 0), 760, 375, 0, "Play (Spacebar)");
al_flip_display();
al_rest(1.5);
al_clear_to_color(al_map_rgb(255, 255, 255));
al_draw_bitmap(bitmap, 0, 0, 0);
al_flip_display();
al_rest(0.5);
}
ALLEGRO_EVENT event;
al_wait_for_event(queue, &event);
if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
running = false;
}
}
As you can see I have this endless loop which blocks the whole program in order for the text to blink. The question is how do I do the blinking so the others things keep working like the event which follows up
(it's here for the window to close, when the user clicks X)
The best way is to check which state (blink on or off) to draw when drawing the text. This can be derived from the current time. Something like:
while (running) {
al_clear_to_color(al_map_rgb(255, 255, 255));
al_draw_bitmap(bitmap, 0, 0, 0);
if (fmod(al_get_time(), 2) < 1.5) { // Show the text for 1.5 seconds every 2 seconds.
al_draw_text(font, al_map_rgb(0, 0, 0), 760, 375, 0, "Play (Spacebar)");
}
al_flip_display();
// Handle events in a non-blocking way, for example
// using al_get_next_event (not al_wait_for_event).
}
Outside your main loop:
Create timer: timer = al_create_timer(...);
Create event queue: event_queue = al_create_event_queue();
At the top within your main loop:
al_wait_for_event(event_queue, &ev);
if (ev.type == ALLEGRO_EVENT_TIMER)
{
// do your blinking stuff here
}

How to create .swf file from multi .JPG images using swflib library in Visual C++?

I'm writing a c++ code to create a animate SWF file from multi JPG pictures.
Now, I have pictures like "image_0" to "image_100". I find a swflib library. I think it will help. So far, I can use this library's method to create .SWF file and the size of .SWF file is the sum of pictures in .JPG format.
So, I think I almost done. But ,SWF does not play. I am crazy.
Below this is the code which I modified:
`void CSWFLIBTestProjectDlg::CreateSWFMovie_Bitmap()
{
// Set movie params
SIZE_F movieSize = {100, 100};
int frameRate = 14;
POINT_F pt;
// Create empty .SWF file
CSWFMovie swfMovie;
swfMovie.OpenSWFFile(_T("SWF Sample Movies/Sample2.swf"), movieSize, frameRate);
SWF_RGB bgColor = {255, 255, 255};
swfMovie.SetBackgroundColor(bgColor);
// Define bitmap object
CSWFBitmap bitmap(2, (UCHAR*)"gif_0.jpg");//bm128
swfMovie.DefineObject(&bitmap, -1, true);
// Define custom shape
RECT_F shapeRect = {0, 0, 1000, 1000};
CSWFShape shape(1, shapeRect, 1);
SWF_RGBA lineColor = {0, 0, 0, 255};
shape.AddLineStyle(0, lineColor);
RECT_F bitmapRect = {0, 0, 1246, 622}; // size of the bitmap
RECT_F clipRect = {0, 0, 100, 100}; // where to fill
shape.AddBitmapFillStyle(bitmap.m_ID, SWF_FILLSTYLETYPE_BITMAP_0, bitmapRect, clipRect);
pt.x = 0;
pt.y = 0;
shape.ChangeStyle(1, 1, 0, &pt);
shape.AddLineSegment(100, 0);
shape.AddLineSegment(0, 100);
shape.AddLineSegment(-100, 0);
shape.AddLineSegment(0, -100);
swfMovie.DefineObject(&shape, shape.m_Depth, true);
swfMovie.ShowFrame();
/****************************************
* move
****************************************/
float i;
for (i=0; i<10; i++)
{
shape.Translate(i, 0);
swfMovie.UpdateObject(&shape, shape.m_Depth, NULL, -1);
swfMovie.ShowFrame();
//if (i>200)
//{
// swfMovie.RemoveObject(shape.m_Depth);
//}
}
/****************************************
* add jpg
****************************************/
swfMovie.RemoveObject(shape.m_Depth);
for (int i=1;i<=100;i++)
{
char filename[100];
sprintf(filename,"gif_%d%s",i,".jpg");
CSWFBitmap bitmap2(3, (UCHAR*)filename);//gif_0
swfMovie.DefineObject(&bitmap2, -1, true);//
// Define custom shape
RECT_F shapeRect2 = {0, 0, 1000, 1000};
CSWFShape shape2(1, shapeRect2, 1);
SWF_RGBA lineColor2 = {0, 0, 0, 255};
shape2.AddLineStyle(0, lineColor2);
RECT_F bitmapRect2 = {0, 0, 1246, 622}; // size of the bitmap
RECT_F clipRect2 = {0, 0, 100, 100}; // where to fill
shape2.AddBitmapFillStyle(bitmap2.m_ID, SWF_FILLSTYLETYPE_BITMAP_0, bitmapRect2, clipRect2);
pt.x = 0;
pt.y = 0;
shape2.ChangeStyle(1, 1, 0, &pt);
shape2.AddLineSegment(100, 0);
shape2.AddLineSegment(0, 100);
shape2.AddLineSegment(-100, 0);
shape2.AddLineSegment(0, -100);
swfMovie.UpdateObject(&shape2, shape2.m_Depth, NULL, -1);
//swfMovie.DefineObject(&shape2, shape2.m_Depth, true);
swfMovie.ShowFrame();
//swfMovie.RemoveObject(shape2.m_Depth);
}
// Close .SWF file
swfMovie.CloseSWFFile();
}`
can you tell me what's wrong with my code ? Thanks in advance.
I have found a way to create a .SWF file from a set of image using C++,and a swflib library.you can download it from the lib file website .in which there is a method named :CreateSWFMovie_Bitmap() here is the code i modified
void CSWFLIBTestProjectDlg::CreateSWFMovie_Bitmap()
{
SIZE_F movieSize = {4000, 4000};
int frameRate = 40;
POINT_F pt;
CSWFMovie swfMovie;
swfMovie.OpenSWFFile(_T("SWF Sample Movies/Sample2.swf"), movieSize, frameRate);
SWF_RGB bgColor = {255, 255, 255};
swfMovie.SetBackgroundColor(bgColor);
CSWFBitmap bitmap(2, (UCHAR*)"bm128.jpg");
swfMovie.DefineObject(&bitmap, -1, false);
RECT_F shapeRect = {0, 0, 1000, 1000};//shape大小
CSWFShape shape(1, shapeRect, 1);
SWF_RGBA lineColor = {0, 0, 0, 255};
shape.AddLineStyle(3, lineColor);
RECT_F bitmapRect = {0, 0, 128, 128}; // size of the bitmap
RECT_F clipRect = {0, 0, 100, 100}; // where to fill
shape.AddBitmapFillStyle(bitmap.m_ID, SWF_FILLSTYLETYPE_BITMAP_0, bitmapRect, clipRect);
pt.x = 0;
pt.y = 0;
shape.ChangeStyle(1, 1, 0, &pt);
shape.AddLineSegment(100, 0);
shape.AddLineSegment(0, 100);
shape.AddLineSegment(-100, 0);
shape.AddLineSegment(0, -100);
swfMovie.DefineObject(&shape, shape.m_Depth, true);
swfMovie.ShowFrame();
for (int i=0; i<100; i++)
{
char filename[50]="";
sprintf(filename,"gif_%d%s",i,".jpg");
CSWFBitmap bitmap2(300+i, (UCHAR*)filename);
swfMovie.DefineObject(&bitmap2, -1, false);//false
CSWFShape shape2(7+i,shapeRect,2+i);//depth值也不能一样 id不能重复
shape2.AddLineStyle(3, lineColor);
RECT_F bitmapRect2 = {0, 0, 128, 128};
RECT_F clipRect2 = {0, 0, 100, 100};
shape2.AddBitmapFillStyle(bitmap2.m_ID, SWF_FILLSTYLETYPE_BITMAP_0, bitmapRect2, clipRect2);
pt.x = 0;
pt.y = 0;
shape2.ChangeStyle(1, 1, 0, &pt);
shape2.AddLineSegment(1000, 0);
shape2.AddLineSegment(0, 1000);
shape2.AddLineSegment(-1000, 0);
shape2.AddLineSegment(0, -1000);
shape2.Translate(80, 0);
swfMovie.DefineObject(&shape2, shape2.m_Depth, true);//必须define才能动 可以不update
swfMovie.ShowFrame();
}
}

Use HBITMAP in dll (Logitech SDK)

I'm writing a plugin for the musicplayer named MusicBee. The plugin is for the Logitech G keyboards LCD. For the Logitech G19 keyboards I will add a nice background from a bitmap file.
The sdk says that I must use HBITMAP to create a background. Now I use the HBITMAP and the background ins't showing. This is the code for the background and logo on the main page of the plugin.
m_lcd.ModifyDisplay(LG_COLOR);
background =(HBITMAP) LoadImage( NULL, L"Logitech/G19logo.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
m_lcd.SetBackground(background);
logo = m_lcd.AddText(LG_STATIC_TEXT, LG_BIG, DT_CENTER, LGLCD_QVGA_BMP_WIDTH);
m_lcd.SetOrigin(logo, 0, 50);
m_lcd.SetText(logo, _T("MusicBee"));
m_lcd.SetTextFontColor(logo, RGB(0,0,0));
m_lcd.Update();
The full code of my Logitech class:
//-----------------------------------------------------------------
// Logitech File
// C++ Source - Logitech.cpp - version 2012 v1.0
//-----------------------------------------------------------------
//-----------------------------------------------------------------
// Include Files
//-----------------------------------------------------------------
#include "stdafx.h"
#include "Logitech.h"
//-----------------------------------------------------------------
// Logitech methods
//-----------------------------------------------------------------
//This LogitechObject is a instance of the Logitech class for using in the thread
Logitech * Logitech::LogitechObject;
Logitech::Logitech(): stopthread(false), firstTime(true), position(0), duration(0)
{
LogitechObject = this;
}
Logitech::~Logitech()
{
stopthread = true;
this->state = StatePlay::Undefined;
timerThread.detach();
}
void CALLBACK Logitech::TimerProc(void* lpParameter, BOOLEAN TimerOrWaitFired)
{
if(LogitechObject->m_lcd.ButtonTriggered(LG_BUTTON_4))
{
LogitechObject->time = 0;
LogitechObject->m_lcd.SetProgressBarPosition(LogitechObject->progressbar, static_cast<FLOAT>(100));
LogitechObject->m_lcd.Update();
}
}
bool Logitech::getFirstTime()
{
return firstTime;
}
//Initialise Logitech LCD
BOOL Logitech::OnInitDialog()
{
HRESULT hRes = m_lcd.Initialize(_T("MusicBee"), LG_DUAL_MODE, FALSE, TRUE);
if (hRes != S_OK)
{
return FALSE;
}
m_lcd.SetAsForeground(true);
//Create home screen Logitech Color LCD
if(m_lcd.IsDeviceAvailable(LG_COLOR))
{
m_lcd.ModifyDisplay(LG_COLOR);
//m_lcd.SetBackground(RGB(245,245,245));
background =(HBITMAP) LoadImage( NULL, L"Logitech/G19logo.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
m_lcd.SetBackground(background);
logo = m_lcd.AddText(LG_STATIC_TEXT, LG_BIG, DT_CENTER, LGLCD_QVGA_BMP_WIDTH);
m_lcd.SetOrigin(logo, 0, 50);
m_lcd.SetText(logo, _T("MusicBee"));
m_lcd.SetTextFontColor(logo, RGB(0,0,0));
m_lcd.Update();
}
//Create home screen Logitech Monochrome LCD
else if(m_lcd.IsDeviceAvailable(LG_MONOCHROME))
{
m_lcd.ModifyDisplay(LG_MONOCHROME);
logo = m_lcd.AddText(LG_STATIC_TEXT, LG_BIG, DT_CENTER, LGLCD_BW_BMP_WIDTH);
m_lcd.SetOrigin(logo, 0, 5);
m_lcd.SetText(logo, _T("MusicBee"));
m_lcd.Update();
}
//Start thread
timerThread = thread(&Logitech::startThread);
//CreateTimerQueueTimer(NULL,NULL,&Logitech::TimerProc,this,0,1250,WT_EXECUTEDEFAULT);
return TRUE; // return TRUE unless you set the focus to a control
}
//Create playing screen for Logitech Monochrome LCD
VOID Logitech::createMonochrome()
{
m_lcd.RemovePage(0);
m_lcd.AddNewPage();
m_lcd.ShowPage(0);
if (logo != 0)
{
delete logo;
logo = 0;
}
artist = m_lcd.AddText(LG_SCROLLING_TEXT, LG_MEDIUM, DT_CENTER, LGLCD_BW_BMP_WIDTH);
m_lcd.SetOrigin(artist, 0, 0);
title = m_lcd.AddText(LG_SCROLLING_TEXT, LG_MEDIUM, DT_CENTER, LGLCD_BW_BMP_WIDTH);
m_lcd.SetOrigin(title, 0, 13);
progressbar = m_lcd.AddProgressBar(LG_FILLED);
m_lcd.SetProgressBarSize(progressbar, 136, 5);
m_lcd.SetOrigin(progressbar, 12, 38);
time = m_lcd.AddText(LG_STATIC_TEXT, LG_SMALL, DT_LEFT, 80);
m_lcd.SetOrigin(time, 12, 29);
time1 = m_lcd.AddText(LG_STATIC_TEXT, LG_SMALL, DT_LEFT, 80);
m_lcd.SetOrigin(time1, 125, 29);
/* playIcon = static_cast<HICON>(LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_PNG2), IMAGE_BITMAP, 16, 16, LR_MONOCHROME));
playIconHandle = m_lcd.AddIcon(playIcon, 16, 16);
m_lcd.SetOrigin(playIconHandle, 2, 29);*/
firstTime = false;
changeArtistTitle(this->artistString, this->albumString, this->titleString, this->duration, this->position);
}
//Create playing screen for Logitech Color LCD
VOID Logitech::createColor()
{
m_lcd.RemovePage(0);
m_lcd.AddNewPage();
m_lcd.ShowPage(0);
if (logo != 0)
{
delete logo;
logo = 0;
}
//background.LoadFromResource(NULL, AfxGetInstanceHandle(), IDB_BITMAP2, _T("BMP"));
background =(HBITMAP) LoadImage( NULL, L"Logitech/G19Background.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
//HBITMAP bmpBkg_ = background.GetHBITMAP();
m_lcd.SetBackground(background);
//m_lcd.SetBackground(RGB(184,220,240));
artist = m_lcd.AddText(LG_SCROLLING_TEXT, LG_MEDIUM, DT_CENTER, LGLCD_QVGA_BMP_WIDTH);
m_lcd.SetOrigin(artist, 5, 5);
m_lcd.SetTextFontColor(artist, RGB(0,0,0));
album = m_lcd.AddText(LG_SCROLLING_TEXT, LG_MEDIUM, DT_CENTER, LGLCD_QVGA_BMP_WIDTH);
m_lcd.SetOrigin(album, 5, 30);
m_lcd.SetTextFontColor(album, RGB(0,0,0));
title = m_lcd.AddText(LG_SCROLLING_TEXT, LG_MEDIUM, DT_CENTER, LGLCD_QVGA_BMP_WIDTH);
m_lcd.SetOrigin(title, 5, 55);
m_lcd.SetTextFontColor(title, RGB(0,0,0));
time = m_lcd.AddText(LG_STATIC_TEXT, LG_SMALL, DT_LEFT, 80);
m_lcd.SetOrigin(time, 5, 80);
m_lcd.SetTextFontColor(time, RGB(0,0,0));
time1 = m_lcd.AddText(LG_STATIC_TEXT, LG_SMALL, DT_LEFT, 40);
m_lcd.SetOrigin(time1, 275, 80);
m_lcd.SetTextFontColor(time1, RGB(0,0,0));
progressbar = m_lcd.AddProgressBar(LG_FILLED);//320�240 pixel color screen
m_lcd.SetProgressBarSize(progressbar, 310, 20);
m_lcd.SetProgressBarColors(progressbar, RGB(25,71,94),NULL);
m_lcd.SetOrigin(progressbar, 5, 100);
/*playIcon = static_cast<HICON>(LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_PNG1), IMAGE_ICON, 16, 16, LR_COLOR));
playIconHandle = m_lcd.AddIcon(playIcon, 16, 16);
m_lcd.SetOrigin(playIconHandle, 5, 29);*/
firstTime = false;
changeArtistTitle(this->artistString, this->albumString, this->titleString, this->duration, this->position);
}
void Logitech::startThread()
{
while(!LogitechObject->stopthread)
{
this_thread::sleep_for( chrono::milliseconds(500) );
if(!LogitechObject->stopthread && LogitechObject->progressbar != NULL)
{
//Update progressbar and position time on the screen after 1 second of music.
if(LogitechObject->state == StatePlay::Playing)
{
this_thread::sleep_for( chrono::milliseconds(500) );
LogitechObject->position++;
float progresstime = ((float)LogitechObject->position / (float)LogitechObject->duration)*100;
LogitechObject->m_lcd.SetProgressBarPosition(LogitechObject->progressbar, static_cast<FLOAT>(progresstime));
LogitechObject->m_lcd.SetText(LogitechObject->time, LogitechObject->getTimeString(LogitechObject->position).c_str());
}
//If music stopped then the progressbar and time must stop immediately
else if(LogitechObject->state == StatePlay::Stopped)
{
LogitechObject->position = 0;
LogitechObject->m_lcd.SetProgressBarPosition(LogitechObject->progressbar, 0);
LogitechObject->m_lcd.SetText(LogitechObject->time, LogitechObject->getTimeString(LogitechObject->position).c_str());
}
LogitechObject->m_lcd.Update();
}
}
}
void Logitech::changeArtistTitle(wstring artistStr, wstring albumStr, wstring titleStr, int duration, int position)
{
this->artistString = artistStr;
this->albumString = albumStr;
this->titleString = titleStr;
this->durationString = getTimeString(duration/1000);
this->position = position;
this->duration = duration/1000;
if(!firstTime)
{
if(m_lcd.IsDeviceAvailable(LG_COLOR))
{
m_lcd.SetText(album, albumStr.c_str());
}
m_lcd.SetText(artist, artistStr.c_str());
m_lcd.SetText(title, titleStr.c_str());
m_lcd.SetText(time, getTimeString(position).c_str());
string s( durationString.begin(), durationString.end() );
if(s.size() < 5)
{
s = "0" + s;
}
wstring ws( s.begin(), s.end() );
m_lcd.SetText(time1, ws.c_str());
ws.clear();
///*playIcon = static_cast<HICON>(LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_PNG1), IMAGE_ICON, 16, 16, LR_COLOR));
//playIconHandle = m_lcd.AddIcon(playIcon, 16, 16);
//m_lcd.SetOrigin(playIconHandle, 5, 29);*/
m_lcd.Update();
artistStr.clear();
albumStr.clear();
titleStr.clear();
}
}
//Set current playing position
void Logitech::setPosition(int pos)
{
this->position = pos/1000;
m_lcd.SetText(time, getTimeString(this->position).c_str());
m_lcd.Update();
}
void Logitech::setDuration(int duration)
{
this->duration = duration/1000;
m_lcd.SetText(time1, getTimeString(this->duration).c_str());
m_lcd.Update();
}
//Change play state of the current playing song
void Logitech::changeState(StatePlay state)
{
this->state = state;
if(state == StatePlay::Playing && firstTime)
{
if(m_lcd.IsDeviceAvailable(LG_COLOR))
{
createColor();
}
else if(m_lcd.IsDeviceAvailable(LG_MONOCHROME))
{
createMonochrome();
}
}
}
//Change int of time to string
wstring Logitech::getTimeString(int time)
{
string minutes = to_string((int)time /60);
string seconds = to_string((int)time%60);
if(minutes.size() < 2)
{
minutes = "0" + minutes;
}
if(seconds.size() < 2)
{
seconds = "0" + seconds;
}
string timeString = minutes + ":" + seconds;
return wstring( timeString.begin(), timeString.end() );
}
What is the problem that the background nog showing?
From your reply in the comments, it seems the problem is with the LoadImage call, so we can ignore the rest of the code for now. Call GetLastError right after the LoadImage call fails and see what it returns.
Only be a few things can go wrong with a LoadImage call like that:
You could have the file name wrong. (GetLastError will return ERROR_FILE_NOT_FOUND)
You're using a relative path, but maybe the current working directory isn't where you think it is. (GetLastError will return ERROR_FILE_NOT_FOUND or ERROR_PATH_NOT_FOUND.)
Maybe that API doesn't work with slashes in place of backslashes.
Maybe you don't have access to read the file. (GetLastError will return ERROR_ACCESS_DENIED or ERROR_NETWORK_ACCESS_DENIED.)
Maybe another application has the file open without sharing. (GetLastError will return ERROR_SHARING_VIOLATION.)
Maybe the file isn't a valid bitmap. (I'm not sure what GetLastError will return here. Perhaps something like ERROR_INVALID_PARAMETER.)
Maybe it's a gigantic bitmap and there's not enough memory to load it. (GetLastError will return ERROR_NOT_ENOUGH_MEMORY.)
In general, when debugging, isolate the point in the code where things go bad (once you know LoadImage failed, you can stop worrying about all the code that happens after that). Then get as much information as you can (e.g., if it's a Windows API call that failed, call GetLastError and re-read the entire MSDN page for that API, use a debugger to examine the relative state at that point). From there, it will usually be pretty obvious what to do. If not, write the simplest program you can that does the same step. If it works, study the differences between your simple example and your actual code.

Loading images from resource don't work(C++ Dll)

I'm writing a Logitech plugin for MusicBee.
The only problem I have is that the images of the artwork, play and pause image don't load.
I have tested a lot of different ways to use the images in the VS2012 resource file. But non of them works. I must load images in a dll file and the image must be HICON or HBITMAP.
Below is een full class of my project. Full project can be found at Bitbucket.
//This is the code I have used for loading an image.
playIcon = LoadIcon (GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1));
playIconHandle = m_lcd.AddIcon(playIcon, 16, 16);
m_lcd.SetOrigin(playIconHandle, 2, 2);
//-----------------------------------------------------------------
// Logitech File
// C++ Source - Logitech.cpp - version 2012 v1.0
//-----------------------------------------------------------------
//-----------------------------------------------------------------
// Include Files
//-----------------------------------------------------------------
#include "stdafx.h"
#include "Logitech.h"
//-----------------------------------------------------------------
// Logitech methods
//-----------------------------------------------------------------
//This LogitechObject is a instance of the Logitech class for using in the thread
Logitech * Logitech::LogitechObject;
Logitech::Logitech(): stopthread(false), firstTime(true), position(0), duration(0)
{
LogitechObject = this;
}
Logitech::~Logitech()
{
stopthread = true;
this->state = StatePlay::Undefined;
timerThread.detach();
}
bool Logitech::getFirstTime()
{
return firstTime;
}
//Initialise Logitech LCD
BOOL Logitech::OnInitDialog()
{
HRESULT hRes = m_lcd.Initialize(_T("MusicBee"), LG_DUAL_MODE, FALSE, TRUE);
if (hRes != S_OK)
{
return FALSE;
}
m_lcd.SetAsForeground(true);
//Create home screen Logitech Color LCD
if(m_lcd.IsDeviceAvailable(LG_COLOR))
{
m_lcd.ModifyDisplay(LG_COLOR);
m_lcd.SetBackground(RGB(245,245,245));
logo = m_lcd.AddText(LG_STATIC_TEXT, LG_BIG, DT_CENTER, LGLCD_QVGA_BMP_WIDTH);
m_lcd.SetOrigin(logo, 0, 50);
m_lcd.SetText(logo, _T("MusicBee"));
m_lcd.SetTextFontColor(logo, RGB(0,0,0));
m_lcd.Update();
}
//Create home screen Logitech Monochrome LCD
else if(m_lcd.IsDeviceAvailable(LG_MONOCHROME))
{
m_lcd.ModifyDisplay(LG_MONOCHROME);
logo = m_lcd.AddText(LG_STATIC_TEXT, LG_BIG, DT_CENTER, LGLCD_BW_BMP_WIDTH);
m_lcd.SetOrigin(logo, 0, 5);
m_lcd.SetText(logo, _T("MusicBee"));
m_lcd.Update();
}
//Start thread
timerThread = thread(&Logitech::startThread);
return TRUE; // return TRUE unless you set the focus to a control
}
//Create playing screen for Logitech Monochrome LCD
VOID Logitech::createMonochrome()
{
m_lcd.RemovePage(0);
m_lcd.AddNewPage();
m_lcd.ShowPage(0);
if (logo != 0)
{
delete logo;
logo = 0;
}
artist = m_lcd.AddText(LG_SCROLLING_TEXT, LG_MEDIUM, DT_CENTER, LGLCD_BW_BMP_WIDTH);
m_lcd.SetOrigin(artist, 0, 0);
title = m_lcd.AddText(LG_SCROLLING_TEXT, LG_MEDIUM, DT_CENTER, LGLCD_BW_BMP_WIDTH);
m_lcd.SetOrigin(title, 0, 13);
progressbar = m_lcd.AddProgressBar(LG_FILLED);
m_lcd.SetProgressBarSize(progressbar, 136, 5);
m_lcd.SetOrigin(progressbar, 12, 38);
time = m_lcd.AddText(LG_STATIC_TEXT, LG_SMALL, DT_LEFT, 80);
m_lcd.SetOrigin(time, 12, 29);
time1 = m_lcd.AddText(LG_STATIC_TEXT, LG_SMALL, DT_LEFT, 80);
m_lcd.SetOrigin(time1, 125, 29);
playIcon = static_cast<HICON>(LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_PNG2), IMAGE_BITMAP, 16, 16, LR_MONOCHROME));
playIconHandle = m_lcd.AddIcon(playIcon, 16, 16);
m_lcd.SetOrigin(playIconHandle, 2, 29);
firstTime = false;
changeArtistTitle(this->artistString, this->albumString, this->titleString, this->duration, this->position);
}
//Create playing screen for Logitech Color LCD
VOID Logitech::createColor()
{
m_lcd.RemovePage(0);
m_lcd.AddNewPage();
m_lcd.ShowPage(0);
if (logo != 0)
{
delete logo;
logo = 0;
}
//background.LoadFromResource(NULL, AfxGetInstanceHandle(), IDB_G19BACKGROUND, _T("PNG"));
//HBITMAP bmpBkg_ = background.GetHBITMAP();
//m_lcd.SetBackground(bmpBkg_);
m_lcd.SetBackground(RGB(184,220,240));
artist = m_lcd.AddText(LG_SCROLLING_TEXT, LG_MEDIUM, DT_CENTER, LGLCD_QVGA_BMP_WIDTH);
m_lcd.SetOrigin(artist, 5, 5);
m_lcd.SetTextFontColor(artist, RGB(0,0,0));
album = m_lcd.AddText(LG_SCROLLING_TEXT, LG_MEDIUM, DT_CENTER, LGLCD_QVGA_BMP_WIDTH);
m_lcd.SetOrigin(album, 5, 30);
m_lcd.SetTextFontColor(album, RGB(0,0,0));
title = m_lcd.AddText(LG_SCROLLING_TEXT, LG_MEDIUM, DT_CENTER, LGLCD_QVGA_BMP_WIDTH);
m_lcd.SetOrigin(title, 5, 55);
m_lcd.SetTextFontColor(title, RGB(0,0,0));
time = m_lcd.AddText(LG_STATIC_TEXT, LG_SMALL, DT_LEFT, 80);
m_lcd.SetOrigin(time, 5, 80);
m_lcd.SetTextFontColor(time, RGB(0,0,0));
time1 = m_lcd.AddText(LG_STATIC_TEXT, LG_SMALL, DT_LEFT, 40);
m_lcd.SetOrigin(time1, 275, 80);
m_lcd.SetTextFontColor(time1, RGB(0,0,0));
progressbar = m_lcd.AddProgressBar(LG_FILLED);//320×240 pixel color screen
m_lcd.SetProgressBarSize(progressbar, 310, 20);
m_lcd.SetProgressBarColors(progressbar, RGB(25,71,94),NULL);
m_lcd.SetOrigin(progressbar, 5, 100);
/*playIcon = static_cast<HICON>(LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_PNG1), IMAGE_ICON, 16, 16, LR_COLOR));
playIconHandle = m_lcd.AddIcon(playIcon, 16, 16);
m_lcd.SetOrigin(playIconHandle, 5, 29);*/
firstTime = false;
changeArtistTitle(this->artistString, this->albumString, this->titleString, this->duration, this->position);
}
void Logitech::startThread()
{
while(!LogitechObject->stopthread)
{
this_thread::sleep_for( chrono::milliseconds(500) );
if(!LogitechObject->stopthread && LogitechObject->progressbar != NULL)
{
//Update progressbar and position time on the screen after 1 second of music.
if(LogitechObject->state == StatePlay::Playing)
{
this_thread::sleep_for( chrono::milliseconds(500) );
LogitechObject->position++;
float progresstime = ((float)LogitechObject->position / (float)LogitechObject->duration)*100;
LogitechObject->m_lcd.SetProgressBarPosition(LogitechObject->progressbar, static_cast<FLOAT>(progresstime));
LogitechObject->m_lcd.SetText(LogitechObject->time, LogitechObject->getTimeString(LogitechObject->position).c_str());
}
//If music stopped then the progressbar and time must stop immediately
else if(LogitechObject->state == StatePlay::Stopped)
{
LogitechObject->position = 0;
LogitechObject->m_lcd.SetProgressBarPosition(LogitechObject->progressbar, 0);
LogitechObject->m_lcd.SetText(LogitechObject->time, LogitechObject->getTimeString(LogitechObject->position).c_str());
}
LogitechObject->m_lcd.Update();
}
}
}
void Logitech::changeArtistTitle(wstring artistStr, wstring albumStr, wstring titleStr, int duration, int position)
{
this->artistString = artistStr;
this->albumString = albumStr;
this->titleString = titleStr;
this->durationString = getTimeString(duration/1000);
this->position = position;
this->duration = duration/1000;
if(!firstTime)
{
if(m_lcd.IsDeviceAvailable(LG_COLOR))
{
m_lcd.SetText(album, albumStr.c_str());
}
m_lcd.SetText(artist, artistStr.c_str());
m_lcd.SetText(title, titleStr.c_str());
m_lcd.SetText(time, getTimeString(position).c_str());
string s( durationString.begin(), durationString.end() );
if(s.size() < 5)
{
s = "0" + s;
}
wstring ws( s.begin(), s.end() );
m_lcd.SetText(time1, ws.c_str());
ws.clear();
///*playIcon = static_cast<HICON>(LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_PNG1), IMAGE_ICON, 16, 16, LR_COLOR));
//playIconHandle = m_lcd.AddIcon(playIcon, 16, 16);
//m_lcd.SetOrigin(playIconHandle, 5, 29);*/
m_lcd.Update();
artistStr.clear();
albumStr.clear();
titleStr.clear();
}
}
//Set current playing position
void Logitech::setPosition(int pos)
{
this->position = pos/1000;
m_lcd.SetText(time, getTimeString(this->position).c_str());
m_lcd.Update();
}
void Logitech::setDuration(int duration)
{
this->duration = duration/1000;
m_lcd.SetText(time1, getTimeString(this->duration).c_str());
m_lcd.Update();
}
//Change play state of the current playing song
void Logitech::changeState(StatePlay state)
{
this->state = state;
if(state == StatePlay::Playing && firstTime)
{
if(m_lcd.IsDeviceAvailable(LG_COLOR))
{
createColor();
}
else if(m_lcd.IsDeviceAvailable(LG_MONOCHROME))
{
createMonochrome();
}
}
}
//Change int of time to string
wstring Logitech::getTimeString(int time)
{
string minutes = to_string((int)time /60);
string seconds = to_string((int)time%60);
if(minutes.size() < 2)
{
minutes = "0" + minutes;
}
if(seconds.size() < 2)
{
seconds = "0" + seconds;
}
string timeString = minutes + ":" + seconds;
return wstring( timeString.begin(), timeString.end() );
}
LoadImage(GetModuleHandle(NULL), ...)
This cannot be correct in a plugin. GetModuleHandle(NULL) returns a handle to the EXE, not your DLL. Assuming you embedded your resources in your plugin DLL, you will need to use the module handle for your DLL, the one you get handed to you in your DllMain() function, 1st argument.