1

Trying to create a background with the above RGB values,however the only success i had till now is by loading jpegs.Is there a way to create the background programmatically?

#include "CImg.h"

using namespace cimg_library;

constexpr int Height = 600;
constexpr int Width = 539;

int main() {
    CImgDisplay mainWindow(Height,Width);
    CImg<unsigned char> background;  
    // background. ??? Is there a function to do so or loading a jpeg the only way
    while(!mainWindow.is_closed()) {
        mainWindow.display(background);
    }

    return EXIT_SUCCESS;
}

EDIT 1: Defined #define cimg_display 2

#define cimg_display 2
#define BACKGROUND_RGB 73,95,105

#include "CImg.h"

using namespace cimg_library;

constexpr int Height = 600;
constexpr int Width = 540;

int main() {
    CImgDisplay mainWindow(Height,Width);
    CImg<unsigned char> background(Height,Width,1,3,BACKGROUND_RGB);
    while(!mainWindow.is_closed()) {
        mainWindow.display(background);
    }

    return EXIT_SUCCESS;
}

Cmakefiles.txt

cmake_minimum_required(VERSION 3.14)
project(Snake__)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static")
include_directories("C:\\C++\\External Library\\CImg-2.6.4\\")
set(CMAKE_CXX_FLAGS "-Wall -Wextra -O3")
add_executable(Snake__ main.cpp)

Compiler Output:

Scanning dependencies of target Snake__
[ 50%] Building CXX object CMakeFiles/Snake__.dir/main.cpp.obj
[100%] Linking CXX executable Snake__.exe
[100%] Built target Snake__

Build finished

1 Answers1

0

Like this:

  CImg<unsigned char> background(600,539,1,3,73,95,105);
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • I changed the code as you said but it is showing a black screen and windows is saying the program has stopped working. Any idea on the matter – Anu Panicker Jul 01 '19 at 19:42
  • Did you link with `gdi32` and define `cimg_display`? – Mark Setchell Jul 01 '19 at 20:03
  • Sorry, I wasn't explicit earlier. You need `#define cimg_display 2` **before** you `#include "CImg.h"` or preferably amongst the compilation switches. – Mark Setchell Jul 01 '19 at 22:09
  • Maybe you could show any error messages by clicking `edit` under your question and pasting them in... – Mark Setchell Jul 02 '19 at 07:56
  • I have uploaded everything and i don't have gdi32.lib on my pc and i use clion and compiler usng mingw 8.1.0 even after #define cimg_display 2 the program is crashing. – Anu Panicker Jul 02 '19 at 09:53
  • Mmmm... not sure about `mingw` and `CLion` as I have not used those before. Also, I would maybe try C++11, rather than C++17. You could also try writing a JPEG of the background image before displaying it, just to see if the file it writes looks correct in another program. – Mark Setchell Jul 02 '19 at 10:24