How to put images on the screen?

Asked

Viewed 1,700 times

1

In MSX, you used BASIC to put an image on the screen without going out of command line mode. Today, we can’t do this anymore? Without using Opengl or other Apis, how do we place pixels on the screen? Or an entire image? Do you have to send it to the VRAM? It is that we only learn to display texts in the DOS of the PC.

2 answers

1

No - nowadays, on regular Pcs there is no way to do this. It happens that since the original VGA specification of the PC two things have happened that prevent you from simply being able to copy a few bytes to a memory region and see this on the screen:

1) A fragmentation of the video mode specifications: after the VGA there was never a unified video mode that worked on all cards. Each manufacturer, alias each video card, had (and somehow still has) specific ways of addressing video memory, video modes, etc... After the video cards surpassed the VGA, and before Windows was the dominant interface for the video, there was a time when each graphics program had to bring, within itself, the drivers for different video cards

2) With the largest capacity of computers (Pcs), the Operating System began to take more seriously its role, until then half theoretical in the Pcs, to abstract the hardware part: the porgrams no longer needed to worry about the video card installed, and used operating system Apis to draw on the screen. With a call to the system, the program discovers the height width of the screen in pixels. If the graphics card didn’t have all the necessary colors, it was S.O. paper - be it Windows, Linux, with the X11 graphics presentation layer, or another system, reduce the number of colors automatically. The operating system also assumes the role of preventing normal programs from writing in any memory region or using I/O to reconfigure the video: only the operating system must be able to change hardware settings - a program should not be allowed to interfere with other programs that are running.


So that’s it - you get the Win 32 API that should allow you to create a full-screen window, and draw in that window. In X11, used in Linux and Unix oturs, you can draw in the Root Window. On Linux and other Unixes ,without the X11 window system, you could also use framebuffer - i.e., fragmentation continues.

A library that appeared after a while, which gives a good hand on this part, entido us to simplify everything, and even allow graphics programs that run in full screen both in Windows and Linux with or without X11 was the SDL - you just need to install the same, and the . h files of it to make code that draw on the screen.

However - if you go to the SDL website, and look for the tutorials, you will see that in C, even using the SDL, has a lot of red tape paté you arrive at the video itself, and be able to draw on the screen.

Many years ago I asked myself the same question you do now: how do I draw on the screen, something that only I want, no windows of other programs, as was done in the Basic of computers of the 80’s?

For me the answer was: using the Python language, and the Python language links with the SDL library - this project is called Pygame.

If you install the Python of the official website, and Pygame appropriate - both have installers for windows, will be able to write code of this type:

import pygame

try:
    pygame.init()
    WIDTH, HEIGHT = pygame.display.list_modes()[0]
    tela = pygame.display.set_mode((1680,1050), pygame.FULLSCREEN)
    #pygame.draw.rect(tela, (255,0,0), (100,100,200,100))
    for y in range(HEIGHT):
        cor = (y % 256, (y + 128) % 256, 255 - y % 256)
        pygame.draw.line(tela, cor, (0,y), (WIDTH,y))
    pygame.event.pump()
    pygame.display.flip()
    pygame.time.delay(10000)
finally:
    pygame.quit()

(this is the complete program) pygame also allows you to upload image files from disk, respond to keyboard and mouse events, among other things - see the full documentation on the site.

0

I believe that the only way will be using some API or Framework, where you would stop using the text terminal to run your applications and start running them in the environment prepared by the API.

You can use GTK or the Borland C++ Builder, or API’s like the Opengl and SDL.

    //Exibindo uma imagem bitmap com SDL
    SDL_Surface *image;
    SDL_Surface *temp;

    temp = SDL_LoadBMP("image.bmp");
    if (temp == NULL) {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 1;
    }

    image = SDL_DisplayFormat(temp);
    SDL_FreeSurface(temp);

I know it’s not exactly the answer you’d like, but I don’t know any other way.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.