Allegro Throws errors when trying to draw lines - c++

For some reason allegro (C++ Game Engine), throws errors when i try to run... So first this code works fine ! :
#include <allegro5/allegro.h>
#include <allegro5/allegro_primitives.h>
int main(void)
{
int width = 640, height = 480;
ALLEGRO_DISPLAY *display = NULL;
if (!al_init())
return -1;
display = al_create_display(width, height);
if (!display)
return -1;
al_flip_display();
//al_draw_line(100, 100, width - 100, 100, al_map_rgb(255, 0, 0), 1);
al_rest(3);
al_destroy_display(display);
return 0;
}
But all the sudden when i try to uncomment that line above i get this "error".
More Closer Up:
As you can see above this is some type of assertion failling... Im Confused?! Help would be appreciated!

Before you can use the primitives add-on you need to initialize it by calling al_init_primitives_addon.
#include <allegro5/allegro.h>
#include <allegro5/allegro_primitives.h>
int main(void)
{
int width = 640, height = 480;
ALLEGRO_DISPLAY *display = NULL;
if (!al_init())
return -1;
display = al_create_display(width, height);
if (!display)
return -1;
al_init_primitives_addon();
if (!al_init_primitives_addon())
return -1;
al_draw_line(0, 50, 300, 100, al_map_rgb(255, 0, 4), 1.0f);
al_flip_display();
al_rest(13);
al_shutdown_primitives_addon();
al_destroy_display(display);
return 0;
}
The full documentation can be found here: https://www.allegro.cc/manual/5/al_init_primitives_addon.
Don't forget to call al_shutdown_primitives_addon when you're done.

Related

i can't show text on the window using SDL2 TTF library

I make a SDL_ttf test code:
#include <SDL.h>
#include <stdbool.h>
#include <iostream>
#include "SDLwindow.h"
#include <SDL_ttf.h>
#include "GraphLib.h"
#undef main
using namespace std;
int main() {
bool running = 1;
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS);
TTF_Init();
WindowSDL window1;
TTF_Font* font = TTF_OpenFont("./acme.ttf", 10);
SDL_Surface* textSurf = TTF_RenderText_Solid(font, "Hola mundo", {255,0,0});
SDL_Texture* textTexture = SDL_CreateTextureFromSurface(window1.renderer, textSurf);
SDL_FreeSurface(textSurf);
SDL_Rect textRect;
textRect.x = 10;
textRect.y = 10;
textRect.w = 400;
textRect.h = 100;
//TTF_CloseFont(font);
window1.CreateWindow("Pix", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 600, 600, SDL_WINDOW_BORDERLESS);
window1.CreateRenderer(window1.window, -1, 0);
window1.ChangeBackgroundColor(0xe0e0e0);
drawLine(window1, 3, 3, 40, 50, 0x0aaf88, 0);
drawLine(window1, 40, 50, 80, 3, 0x0aaf88, 0);
drawLine(window1, 80, 3, 3, 3, 0x0aaf88, 0);
SDL_RenderCopy(window1.renderer, textTexture, NULL, &textRect);
SDL_RenderPresent(window1.renderer);
while (running) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
running = 0;
}
}
}
window1.Shutdown();
TTF_Quit();
return 0;
}
...and... boom it doesn't appear, heres my screen:
I tried to change the order of certain lines like TTF_Init(); and so on, the results were the same, it does not appear on the screen

X11 won't open a window

I am trying to open an X11 window, print out one pixel, and then later add code to make Terminate() return true. But it won't make a window. Here is my code:
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <iostream>
int x = 0;
int y = 0;
bool Termination() {
return true;
}
int main() {
Display *dspl = XOpenDisplay(NULL);
if (!dspl) return 1;
int screenNumber = DefaultScreen(dspl);
unsigned long white = WhitePixel(dspl, screenNumber);
unsigned long black = BlackPixel(dspl, screenNumber);
Window win = XCreateSimpleWindow(dspl, DefaultRootWindow(dspl), 50, 50, 1280, 720, 0, black, white);
XSetStandardProperties(dspl, win, "Lel", "Gaem", None, NULL, 0, NULL);
GC gc = XCreateGC(dspl, win, 0,0);
XSetBackground(dspl, gc, black);
XSetForeground(dspl, gc, white);
XClearWindow(dspl, win);
XMapRaised(dspl, win);
XDrawPoint(dspl, win, gc, x, y);
while (Termination())
{
}
XFreeGC(dspl, gc);
XDestroyWindow(dspl, win);
XCloseDisplay(dspl);
printf("Job's done!\n");
return 0;
}
What's keeping X11 from making my window appear?
add this before the while
XMapWindow(dspl, win);
XInternAtom(dspl, "WM_DELETE_WINDOW", False);

No matching function for call to 'SDL_UpperBlit'

I'm relatively new to c++, so despite my experience in other c languages this may just be a simple mistake. Here's my code:
#include <iostream>
#include <SDL2/SDL.h>
SDL_Window *window;
SDL_Surface *windowSurface;
SDL_Surface *image[3];
SDL_Surface *currentImage;
int close();
SDL_Rect gRect(int, int, int, int); //for method described below
int main(int argc, char *argv[]){
SDL_Init(SDL_INIT_VIDEO);
window = SDL_CreateWindow("Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 500, 500, SDL_WINDOW_SHOWN);
windowSurface = SDL_GetWindowSurface(window);
image[0] = SDL_LoadBMP("filepath/img.bmp");
image[1] = SDL_LoadBMP("filepath/img2.bmp");
image[2] = SDL_LoadBMP("filepath/img3.bmp");
currentImage = image[0];
bool done = false;
while(!done){
SDL_Rect rec;
rec.x = 100;
rec.y = 100;
rec.h = 100;
rec.w = 100;
SDL_BlitSurface(currentImage, NULL, windowSurface, rec);
SDL_UpdateWindowSurface(window);
}
return close();
}
int close(){
for(int i = 0; i < 3; i++){
SDL_FreeSurface(image[i]);
image[i] = nullptr;
}
currentImage = nullptr;
window = nullptr;
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
Upon running this program I receive the error "No matching function for call to "SDL_UpperBlit"". Apparently this has been replaced with SDL_BlitSurface, which I am using in my program.
This error does not appear if I replace "rec" in the function call with "NULL", and my program compiles like normal, with the image filling the screen.
I tried using a function to create an SDL_Rect like so:
SDL_Rect gRect(int x, int y, int w, int h){
SDL_Rect ret;
ret.h = h;
ret.w = w;
ret.x = x;
ret.y = y;
return ret;
}
and changing the SDL_BlitSurface call to
SDL_BlitSurface(currentImage, NULL, windowSurface, gRect(100, 100, 100, 100));
This does not resolve the issue and the same error appears. If I could get any help with this it would be greatly appreciated! Thanks!
(Optional) PS: I also would appreciate some help with putting the "image[0]", "image[1]", and "image[2]" declarations in a for loop. I tried it but had some issues with types and concatenation so if anyone knows how I could do this let me know.
SDL_BlitSurface function declaration is
int SDL_BlitSurface(SDL_Surface* src,
const SDL_Rect* srcrect,
SDL_Surface* dst,
SDL_Rect* dstrect)
Your code fragment
SDL_Rect rec;
rec.x = 100;
rec.y = 100;
rec.h = 100;
rec.w = 100;
SDL_BlitSurface(currentImage, NULL, windowSurface, rec);
passes rec as last argument, which have type SDL_Rect, but SDL_Rect* is expected. It should be e.g. SDL_BlitSurface(currentImage, NULL, windowSurface, &rec);
I would also advice against using close as function name as it is known to cause problems (not as much true for C++ as it have name mangling, but still).

How to properly use SDL_BlitSurface() with SDL_CreateRGBSurface()?

(See "Edit 2" below for the solution.)
I need to create SDL surfaces from scratch, instead of loading them from a file. Unfortunately, SDL_BlitSurface() seems to render all colors as black when used with the surface generated through SDL_CreateRGBSurface(). This is my code:
int main(int argc, char** argv)
{
SDL_Surface* screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE);
SDL_Surface* layer = SDL_CreateRGBSurface(SDL_HWSURFACE, 100, 100,
screen->format->BitsPerPixel,
screen->format->Rmask,
screen->format->Gmask,
screen->format->Bmask,
screen->format->Amask
);
SDL_Rect rect;
rect.x = 0;
rect.y = 0;
rect.w = 100;
rect.h = 100;
Uint32 blue = SDL_MapRGB(screen->format, 0, 0, 255);
SDL_FillRect(layer, &rect, blue);
SDL_BlitSurface(screen, NULL, layer, NULL);
SDL_Flip(screen);
SDL_Delay(3000);
return 0;
}
What I get is a black screen, instead of a 100x100 blue rectangle. What I could find by Googling doesn't seem to help me, as those questions either apply to 8bit surfaces (and setting palettes — my bpp is 32 here) or are left unanswered.
So, I would like to know how should I properly blit a generated surface onto a SDL screen.
Edit: I see it was an error in the parameter ordering. The line in question should read
SDL_BlitSurface(layer, NULL, screen, NULL);
Still, I am having trouble to achieve the same effect in my more complex C++ program. I will post the relevant parts of the code here:
main.cpp:
int main(int argc, char** argv)
{
SDLScreen screen(1024, 700, "Hello, SDL!");
SDL_Event event;
SDLMenu menu;
bool shouldQuit = false;
menu.setBounds(200, 100, 200, 600);
menu.setFontName("NK211.otf");
menu.setFontSize(36);
menu.setEffect(sdlteShadowText);
menu.addItem("New game");
menu.addItem("Load game");
menu.addItem("Save game");
menu.addItem("Exit");
menu.render();
while (!shouldQuit)
{
menu.draw(screen.getSurface());
SDL_Flip(screen.getSurface());
SDL_Delay(10);
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
shouldQuit = true;
}
else if (event.type == SDL_KEYUP)
{
if (event.key.keysym.sym == SDLK_q)
{
shouldQuit = true;
}
}
}
}
}
SDLMenu.cpp:
void
SDLMenu::setSelectionColorRGB(int r, int g, int b)
{
SDL_VideoInfo* info = (SDL_VideoInfo*)SDL_GetVideoInfo();
selectionColor = SDL_MapRGB(info->vfmt, r, g, b);
}
void
SDLMenu::render()
{
SDLText* current = NULL;
SDL_VideoInfo* info = (SDL_VideoInfo*)SDL_GetVideoInfo();
if (!items->empty())
{
current = getItemAt(currentItem);
selectionRect = getItemRect(current);
setSelectionColorRGB(0,0,255);
selectionCanvas = SDL_CreateRGBSurface(SDL_HWSURFACE,
selectionRect->w, selectionRect->h,
info->vfmt->BitsPerPixel,
info->vfmt->Rmask,
info->vfmt->Gmask,
info->vfmt->Bmask,
info->vfmt->Amask);
SDL_FillRect(selectionCanvas, selectionRect, selectionColor);
SDL_SaveBMP(selectionCanvas, "selection.bmp"); // debug
}
for (list<SDLText*>::iterator i = items->begin();
i != items->end(); i++)
{
(*i)->render();
}
}
void
SDLMenu::draw(SDL_Surface* canvas)
{
int currentY = bounds.y;
if (selectionCanvas != NULL)
{
SDL_BlitSurface(selectionCanvas, NULL, canvas, selectionRect);
}
for (list<SDLText*>::iterator i = items->begin();
i != items->end(); i++)
{
(*i)->draw(bounds.x, currentY, canvas);
currentY += fontSize + itemGap;
}
}
SDLScreen.cpp:
SDLScreen::SDLScreen(int w, int h, string t, int d)
: width(w), height(h), depth(d), title(t)
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_WM_SetCaption(title.c_str(), NULL);
refresh();
}
void
SDLScreen::refresh()
{
screen = SDL_SetVideoMode(width, height, 32, SDL_HWSURFACE);
}
The selection rectangle for the active menu item should be blue, but it shows up in black. The file selection.bmp is also all black.
Edit 2: I found out what created the problem. The selectionRect was set relative to the screen, while the selectionCanvas had the width and height of a particular menu item. So, the filling was done out of bounds of the selectionCanvas. Adding separate SDL_Rect for filling solved the problem.
SDL_Rect fillRect;
fillRect.x = 0;
fillRect.y = 0;
fillRect.w = selectionRect->w;
fillRect.h = selectionRect->h;
SDL_FillRect(selectionCanvas, &fillRect, selectionColor);
// and later...
SDL_BlitSurface(selectionCanvas, NULL, canvas, selectionRect);
You inverted source and destination. To blit on screen, it should be
SDL_BlitSurface(layer, NULL, screen, NULL);
doc for SDL_BlitSurface

how do you make a background in allegro with c++?

i'm fairly new at programming with allegro, and i wanna change the background color of my programs from something more pleasant than black haha :) can some one help please?
and just for a reference of what im doing
#include <allegro.h>
BITMAP* buffer;
BITMAP* bmp;
int cursor_x = 20;
int cursor_y = 20;
int getMouseInfo(){
if(mouse_b & 1){
cursor_x = mouse_x;
cursor_y = mouse_y;
return 1;
}
return 0;
}
void updateScreen(){
show_mouse(NULL);
circlefill ( buffer, cursor_x, cursor_y, 60, makecol( 0, 255 , 0));
draw_sprite( screen, buffer, 0, 0);
}
int main(){
allegro_init();
install_mouse();
install_keyboard();
set_color_depth(16);
set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);
rectfill (
buffer = create_bitmap( 640, 480);
show_mouse(screen);
while( !key[KEY_ESC])
{
int switcher=1;
while(getMouseInfo())
{
updateScreen();
if(getMouseInfo()==0) switcher=0;
}
if(switcher==0) show_mouse(screen);
}
return 0;
}
END_OF_MAIN();
To create backgroud bitmap try this:
/* Make a bitmap in RAM. */
BITMAP *bmp = create_bitmap(SCR_X, SCR_Y);
then try this to clear bmp to some different color:
/* Clear the screen to red. */
clear_to_color(bmp, makecol(255, 0, 0));
or this to load bitmap from file:
bmp = load_bitmap("image.pcx", palette);
Then you just need to blit this bitmap with your screen - like this:
/* Blit bmp on the screen. */
blit(bmp, screen, 0, 0, 0, 0, bmp->w, bmp->h);
Draw a screen-sized rectangle that is the color you want the background to be. Or just use clear_bitmap to clear the screen.
#include <iostream>
using namespace std;
int main()
{
cout<<" In the world were gamers play MINECRAFT, where they creating everithing they can emagine ...\n";
cin.get();
return 0;
}