Rendering and capturing screenshot on a PC with no monitor

0

According to my previous question, I use the following code to take screenshot of my screen. However, I use a PC which connects to no monitor. Then this method captures a black screen. Is there any method to force rendering and capturing image on a computer with no monitor?

I prefer a solution based on code rather than based on operating system as a few of my attempts failed.

lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 16.04.4 LTS
Release:    16.04
Codename:   xenial

Code:

#include "SDL/SDL.h"
#include <iostream>
#include <GL/freeglut.h>
#include <SDL2/SDL.h>

#define SDL_LOCKIFMUST(s) (SDL_MUSTLOCK(s) ? SDL_LockSurface(s) : 0)
#define SDL_UNLOCKIFMUST(s) { if(SDL_MUSTLOCK(s)) SDL_UnlockSurface(s); }

int invert_surface_vertical(SDL_Surface *surface)
{
    Uint8 *t;
    register Uint8 *a, *b;
    Uint8 *last;
    register Uint16 pitch;

    if( SDL_LOCKIFMUST(surface) < 0 )
        return -2;

    /* do nothing unless at least two lines */
    if(surface->h < 2) {
        SDL_UNLOCKIFMUST(surface);
        return 0;
    }

    /* get a place to store a line */
    pitch = surface->pitch;
    t = (Uint8*)malloc(pitch);

    if(t == NULL) {
        SDL_UNLOCKIFMUST(surface);
        return -2;
    }

    /* get first line; it's about to be trampled */
    memcpy(t,surface->pixels,pitch);

    /* now, shuffle the rest so it's almost correct */
    a = (Uint8*)surface->pixels;
    last = a + pitch * (surface->h - 1);
    b = last;

    while(a < b) {
        memcpy(a,b,pitch);
        a += pitch;
        memcpy(b,a,pitch);
        b -= pitch;
    }

    /* in this shuffled state, the bottom slice is too far down */
    memmove( b, b+pitch, last-b );

    /* now we can put back that first row--in the last place */
    memcpy(last,t,pitch);

    /* everything is in the right place; close up. */
    free(t);
    SDL_UNLOCKIFMUST(surface);

    return 0;
}

void screen_shot(std::string filename)
{
    int width = glutGet(GLUT_WINDOW_WIDTH);
    int height = glutGet(GLUT_WINDOW_HEIGHT);

    SDL_Surface * image = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 24, 0x000000FF, 0x0000FF00, 0x00FF0000, 0);

    glReadBuffer(GL_FRONT);
    glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, image->pixels);

    invert_surface_vertical(image);
    SDL_SaveBMP(image, filename.c_str());
    SDL_FreeSurface(image);
}

void cback_render()
{
    if(!glutGetWindow())
        return ;
    static float rotations = 0;

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glClearColor(1.0f, 0.0f, 0.0f, 1.0f);

    glLoadIdentity();
    glRotatef(rotations, 0, 0, 1);

    glBegin(GL_TRIANGLES);
        glVertex3f(0,0,0);
        glVertex3f(1,0,0);
        glVertex3f(0,1,0);
    glEnd();

    glutSwapBuffers();
}

void timer(int )
{
    if(!glutGetWindow())
        return ;
    static bool saved=false;
    if(!saved)
    {
        screen_shot("image.bmp");
        saved=true;
    }
    glutPostRedisplay();
    glutMainLoopEvent();
    glutTimerFunc(30, timer, 1);
}

void init()
{
    int argc=1;
    glutInit(&argc, NULL);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(512, 512);
    glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS);

    glutCreateWindow("freegluttest");
    glutDisplayFunc (cback_render);
    glutTimerFunc(30, timer, 1);
}

int main()
{
    init();
    glutMainLoop();

    return 0;
}

P.S. Not interested in the solution for the linked question.

In addition, my ffmpeg fails initializing off-srceen.

Even if I totally omit ffmpeg, this code gives me Segmentation fault (core dumped) at nowhere (compiled via -O0 -g3 options):

(gdb) bt
#0  0x0000000000000000 in ?? ()
#1  0x0000000000419623 in ?? ()
#2  0x00007f6ec4733510 in ?? ()
#3  0x000000016eccea00 in ?? ()
#4  0x00000000004351d0 in ?? ()
#5  0x68f012126eccea00 in ?? ()
#6  0x000000000063ca60 in ?? ()
#7  0x00000000004125cb in ?? ()
#8  0x000000000063cd60 in ?? ()
#9  0x00007ffd9b2b87c8 in ?? ()
#10 0x0000000000000000 in ?? ()
c++
opengl
rendering
screenshot
sdl-2
asked on Stack Overflow May 8, 2018 by ar2015 • edited May 8, 2018 by ar2015

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0