if else if not working 'Javascript ' - if-statement

function checkforblank(){
if (document.getElementById('fname').value == "help","help ","HELP","HELP ") {
showText("#msg2", 'commands:', 0, 100);
showText("#msg3", '#1 MAIN, back to MAIN menu', 0, 100);
showText("#msg4", '#2 PORT, nav to PORT page', 0, 100);
showText("#msg5", '#3 ABOUT, nav to ABOUT page', 0, 100);
showText("#msg6", '#4 CONT, nav to CONTACT page', 0, 100);
showText("#msg7", '#5 STOP, stop all', 0, 100);
//(document.getElementById('textarea').document.write('jajajajajjaja'());
return false;
}
else if (document.getElementById('fname').value == "main","main ","MAIN","MAIN "){
alert('hello');
}
else{
alert('error');
return false;
}
}
my else if statement doesnt execute code but instead it does the code in the if statement..

This one doesn't work. You have to separate all different values with the OR operator ||.
if (document.getElementById('fname').value == "help" || document.getElementById('fname').value == "HELP ") {

Related

Repositioning of MFCToolBars

I am using Visual Studio 2019, I created an MDI application.
I have placed 4 toolbars in a row in CMainFrame (derived from CMDIFrameWndEx).
Depending on the active view I hide one toolbar, and thus it’s possible have a gap
between two toolbars.
How can I close the gap automatically by attaching the right toolbar to the left?
Here is my code:
LRESULT CMainFrame::OnActivateViewSelect(WPARAM w, LPARAM l)
{
if (m_bViewInit)
{
auto iViewSelect = _S32(w);
if (iViewSelect != m_lastpane)
{
m_lastpane = iViewSelect;
ShowPane(&m_wndToolBar[0], iViewSelect == 0, FALSE, iViewSelect == 0);
ShowPane(&m_wndToolBar[1], iViewSelect == 1, FALSE, iViewSelect == 1);
ShowPane(&m_wndToolBar[2], iViewSelect == 2, FALSE, iViewSelect == 2);
ShowPane(&m_wndToolBar[3], iViewSelect == 3, FALSE, iViewSelect == 3);
RecalcLayout();
}
}
return 0L;
}

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! :)

Allegro5 window won't close

I have a window in allegro, and when the X button at the top is clicked it should close. I have all the necessary code for it to work, but it won't.
To initialize the display I have this:
display = al_create_display(dwidth, dheight);
if (!display){
error.message("Fatal Error", "ERROR:", "DISPLAY HAS FAILED TO BE CREATED");
}
To initialize the event queue I have this:
ALLEGRO_EVENT_QUEUE *event_queue = NULL;
event_queue = al_create_event_queue();
if (!event_queue){
error.message("Fatal Error", "ERROR:", "EVENT QUEUE HAS FAILED TO BE CREATED");
}
al_register_event_source(event_queue, al_get_display_event_source(display));
And to respond to the input and render with or close the window I have this:
al_start_timer(tick);
while (true)
{
//handle input and timer
ALLEGRO_EVENT ev;
al_wait_for_event(event_queue, &ev);
if (ev.type = ALLEGRO_EVENT_TIMER){
redraw = true;
//put all fps dependant function here
}
else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE){
break;
}
if (redraw && al_is_event_queue_empty(event_queue)) {
//FPS independant functions go here
al_flip_display();
al_clear_to_color(al_map_rgb(255, 255, 255));
redraw = false;
}
}
I think you need to change the line:
else if (ev.type == ALLEGRO_EVENT_KEY_DOWN){
to
else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE){

Need this to play music

Game dev class. Trying to understand what i did wrong here. Did I add the wrong thing, or do i have it at the wrong place.
my goal was to add music.
#include "allegro5/allegro.h"
#include <allegro5/allegro_image.h>
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_ttf.h>
#include <allegro5/allegro_native_dialog.h>
#include <stdio.h>
#include <string>
#include <allegro5\allegro_audio.h>
#include <allegro5\allegro_acodec.h>
#define FPS 60
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
#define BLACK al_map_rgb(0, 0, 0)
#define WHITE al_map_rgb(255, 255, 255)
//Prototypes
bool initializeAllegro();
//Essential Allegro pointers
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_EVENT_QUEUE *eventQueue = NULL;
ALLEGRO_TIMER *timer = NULL;
ALLEGRO_FONT *arial14;
int main()
{
ALLEGRO_BITMAP *backgroundImage; //Bitmap for the background image(star field)
int backgroundImageWidth = 0, backgroundImageHeight = 0;
bool redrawRequired = true;
//Using std:string for name, so length is not a factor
std::string userName = "";
std::string prompt1 = "Enter your Player's name below";
std::string prompt2 = "(Letters & digits only. Backspace to erase.)";
std::string prompt3 = "Press Enter to accept...";
std::string prompt4 = "Press Escape to exit...";
char keyToAdd = ' ';
bool enteringUserName = true;
bool quit = false;
//Initialize allegro, etc
if (!initializeAllegro()) return -1;
//load the arial font
arial14 = al_load_font("arial-mt-black.ttf", 14, 0);
if (!arial14)
{
return -3;
}
//test running music
al_init_acodec_addon();
ALLEGRO_SAMPLE *song = al_load_sample("hideandseek.wav");
ALLEGRO_SAMPLE_INSTANCE *songInstance = al_create_sample_instance(song);
al_set_sample_instance_playmode(songInstance, ALLEGRO_PLAYMODE_LOOP);
al_attach_sample_instance_to_mixer(songInstance, al_get_default_mixer());
//Load the background picture
if (!(backgroundImage = al_load_bitmap("starbackground.bmp")))
{
return -5;
}
//Get dimensions of the background image
backgroundImageWidth = al_get_bitmap_width(backgroundImage);
backgroundImageHeight = al_get_bitmap_height(backgroundImage);
//Set the back buffer as the drawing bitmap for the display
al_set_target_bitmap(al_get_backbuffer(display));
//Initialize the event queue
eventQueue = al_create_event_queue();
if (!eventQueue)
{
al_destroy_display(display);
al_destroy_timer(timer);
return -1;
}
//Register events as arriving from these sources: display, timer, keyboard
al_register_event_source(eventQueue, al_get_display_event_source(display));
al_register_event_source(eventQueue, al_get_timer_event_source(timer));
al_register_event_source(eventQueue, al_get_keyboard_event_source());
al_flip_display();
//play song
al_play_sample_instance(songInstance);
//Start up the timer. Don't do this until just before the game is to start!
al_start_timer(timer);
//This would be a loop solely for entering the user's name, not starting the game
//Allegro keycodes are laid out as follows. (Full set of key codes is included in keycodes.h)
//Key codes for capital letters are 1 - 26 for A - Z
//Digit values are 27 - 36 for the top digits (0 - 9), and 37 - 46 for the keypad
while (!quit)
{
ALLEGRO_EVENT evt;
//Wait for one of the allegro-defined events
al_wait_for_event(eventQueue, &evt);
//An event was generated. Check for all possible event types
switch (evt.type)
{
case ALLEGRO_EVENT_KEY_CHAR:
if (evt.keyboard.keycode >= 1 && evt.keyboard.keycode <= 26) //It's a capital letter
{
//Convert to ASCII capital
keyToAdd = *al_keycode_to_name(evt.keyboard.keycode);
userName += keyToAdd;
}
else if (evt.keyboard.keycode >= 27 && evt.keyboard.keycode <= 36)
{
//Convert to digit
keyToAdd = evt.keyboard.keycode + 21;
userName += keyToAdd;
}
//Handle digits on keypad
else if (evt.keyboard.keycode >= 37 && evt.keyboard.keycode <= 46)
{
//Convert to digit
keyToAdd = evt.keyboard.keycode + 11;
userName += keyToAdd;
}
else //Handle a few other keys
{
switch(evt.keyboard.keycode)
{
case ALLEGRO_KEY_BACKSPACE:
if (userName.length() > 0)
userName = userName.substr(0, userName.length() - 1);
break;
//Enter key stops username entry
case ALLEGRO_KEY_ENTER:
case ALLEGRO_KEY_PAD_ENTER:
enteringUserName = false;
break;
case ALLEGRO_KEY_ESCAPE:
quit = true;
break;
}
}
break;
case ALLEGRO_EVENT_DISPLAY_CLOSE:
quit = true;
break;
case ALLEGRO_EVENT_TIMER:
redrawRequired = true;
break;
}//END switch evt.type
//Rerender the entire scene
if (redrawRequired && al_is_event_queue_empty(eventQueue))
{
redrawRequired = false;
al_clear_to_color(BLACK); //Just in case
//Draw background
al_draw_scaled_bitmap(backgroundImage, 0, 0, backgroundImageWidth, backgroundImageHeight,
0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0);
//Prompt for name to be entered
al_draw_textf (arial14, WHITE, 0, 30, 0, "%s", prompt1.c_str());
al_draw_textf (arial14, WHITE, 0, 50, 0, "%s", prompt2.c_str());
if (userName.length() > 0)
{
if (enteringUserName)
{
al_draw_textf (arial14, WHITE, 0, 70, 0, "%s", userName.c_str());
al_draw_textf (arial14, WHITE, 0, 90, 0, "%s", prompt3.c_str());
}
else
{
al_draw_textf (arial14, WHITE, 0, 70, 0, "You entered your name as-->%s", userName.c_str());
}
}
al_draw_textf(arial14, WHITE, 0, 400, 0, "%s", prompt4.c_str());
al_flip_display();
}//END rendering the screen
}//END input Loop
//Release all dynamically allocated memory
al_destroy_bitmap(backgroundImage);
al_destroy_font(arial14);
al_destroy_timer(timer);
al_destroy_display(display);
al_destroy_event_queue(eventQueue);
//destroy songs
al_destroy_sample(song);
al_destroy_sample_instance(songInstance);
return 0;
}//END main
//Take care of Allegro initialization tasks
//Return false if any fail.
bool initializeAllegro()
{
bool success = true;
//Init basic environment
if (!al_init())
{
al_show_native_message_box(NULL, "ERROR", "Allegro failed to initialize!", NULL, NULL, NULL);
success = false;
}
//Initialize keyboard input
if (!al_install_keyboard())
{
al_show_native_message_box(NULL, "ERROR", "Keyboard failed to initialize!", NULL, NULL, NULL);
success = false;
}
//Initialize timer
timer = al_create_timer(1.0 / FPS);
if (!timer)
{
al_show_native_message_box(NULL, "ERROR", "Timer failed to initialize!", NULL, NULL, NULL);
success = false;
}
//Initialize display
display = al_create_display(SCREEN_WIDTH, SCREEN_HEIGHT);
if (!display)
{
al_show_native_message_box(NULL, "ERROR", "Display failed to initialize!", NULL, NULL, NULL);
success = false;
}
//Initialize the image mechanism
if (!al_init_image_addon())
{
al_show_native_message_box(NULL, "ERROR", "Image addon failed to initialize!", NULL, NULL, NULL);
success = false;
}
al_init_font_addon();
al_init_ttf_addon();
return success;
}//END initializeAllegro
Any wav file should work.
i get the error message -
First-chance exception at 0x0F196486
(allegro-5.0.10-monolith-md-debug.dll) in User Input Project.exe:
0xC0000005: Access violation reading location 0x00000000. Unhandled
exception at 0x0F196486 (allegro-5.0.10-monolith-md-debug.dll) in User
Input Project.exe: 0xC0000005: Access violation reading location
0x00000000.
The program '[9596] User Input Project.exe' has exited with code 0
(0x0).
I mentioned to step through things with a debugger in my comment, but the code smell here is this:
ALLEGRO_SAMPLE *song = al_load_sample("hideandseek.wav");
ALLEGRO_SAMPLE_INSTANCE *songInstance = al_create_sample_instance(song);
You should Definitely be checking for (song == NULL) before you call al_create_sample_instance()
If your path to the .WAV file is wrong or if the loading fails for any other reason, then al_load_sample() will return NULL, and you shouldn't be trying to do anything with song

Xlib: Closing window always causes fatal IO error?

I'm not sure why this happens, but any window I create using Xlib in C++ gives outputs an error to the terminal when I try to close is using the X button. I can close it programmatically with no errors, it's just the X button that does it.
The error is the following:
XIO: fatal IO error 11 (Resource temporarily unavailable) on X server ":0"
after 483 requests (483 known processed) with 0 events remaining.
The number of requests is different every time, but there's always 0 events remaining. Why does this happen? The cause doesn't seem to be my code, since it does this no matter what and sends no close events to the queue. I've tried intercepting the Atom WM_WINDOW_DELETE, and it doesn't run over the expected code when I close the window.
Edit: Added event loop code.
while(XPending(display)) {
XNextEvent(display, &event);
pthread_mutex_unlock(&mutex);
if(event.type == Expose) {
XWindowAttributes getWindowAttributes;
pthread_mutex_lock(&mutex);
XGetWindowAttributes(display, window, &getWindowAttributes);
if(state.currentState == STATE_NORMAL) {
state.normX = getWindowAttributes.x;
state.normY = getWindowAttributes.y;
state.normWidth = getWindowAttributes.width;
state.normHeight = getWindowAttributes.height;
}
pthread_mutex_unlock(&mutex);
glViewport(0, 0, getWindowAttributes.width, getWindowAttributes.height);
} else if(event.type == KeyPress) {
return false;
} else if(event.type == ClientMessage) {
std::cout<<"X Button pressed"<<std::endl; //Never run when X-ing window
if(event.xclient.message_type == XInternAtom(display, "WM_DELETE_WINDOW", True)) {
return false;
}
} else if(event.type == ButtonPress) {
if(state.currentState != STATE_FULLSCREEN) {
fullscreen();
} else {
normalize();
}
} else if(!handleEvent(event)){
return false;
}
pthread_mutex_lock(&mutex);
}
In addition to WM_WINDOW_DELETE you need to listen for and handle the ClientMessage event.
Modifying the example from Rosetta Code to illustrate:
#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
Display *d;
Window w;
XEvent e;
const char *msg = "Hello, World!";
int s;
d = XOpenDisplay(NULL);
if (d == NULL) {
fprintf(stderr, "Cannot open display\n");
exit(1);
}
s = DefaultScreen(d);
w = XCreateSimpleWindow(d, RootWindow(d, s), 10, 10, 100, 100, 1, BlackPixel(d, s), WhitePixel(d, s));
XSelectInput(d, w, ExposureMask | KeyPressMask);
XMapWindow(d, w);
// I support the WM_DELETE_WINDOW protocol
Atom WM_DELETE_WINDOW = XInternAtom(d, "WM_DELETE_WINDOW", False);
XSetWMProtocols(d, w, &WM_DELETE_WINDOW, 1);
while (1) {
XNextEvent(d, &e);
if (e.type == Expose) {
XFillRectangle(d, w, DefaultGC(d, s), 20, 20, 10, 10);
XDrawString(d, w, DefaultGC(d, s), 10, 50, msg, strlen(msg));
}
else if (e.type == KeyPress)
break;
else if (e.type == ClientMessage)
// TODO Should check here for other client message types -
// however as the only protocol registered above is WM_DELETE_WINDOW
// it is safe for this small example.
break;
}
XCloseDisplay(d);
return 0;
}