2

I am starting with SDL, and I was reading the introduction, and I am trying the drawPixel method they have. What I am doing is a ppm viewer, so far I have the rgb values in an array and are correctly stored (i checked them by printing the array and making sure they correspond to their position in the ppm file) and I want to use SDL to draw the picture. So far the code I've written is (this is the main.cpp file, if ppm.hpp and ppm.cpp are needed please tell me so to add them)

#include <iostream>
#include <SDL/SDL.h>

#include "ppm.hpp"

using namespace std;

void drawPixel (SDL_Surface*, Uint8, Uint8, Uint8, int, int);

int main (int argc, char** argv) {
    PPM ppm ("res/cake.ppm");

    if (SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO) < 0) {
        cerr << "Unable to init SDL: " << SDL_GetError() << endl;
        exit(1);
    }

    atexit(SDL_Quit); // to automatically call SDL_Quit() when the program terminates

    SDL_Surface* screen;
    screen = SDL_SetVideoMode(ppm.width(), ppm.height(), 32, SDL_SWSURFACE);
    if (screen == nullptr) {
        cerr << "Unable to set " << ppm.width() << "x" << ppm.height() << " video: " << SDL_GetError() << endl;
        exit(1);
    }

    for (int i = 0; i < ppm.width(); i++) {
        for(int j = 0; j < ppm.height(); j++) {
            drawPixel(screen, ppm.red(i,j), ppm.green(i,j), ppm.blue(i,j), i, j);
        }
    }

    return 0;
}

void drawPixel (SDL_Surface* screen, Uint8 R, Uint8 G, Uint8 B, int x, int y) {
    Uint32 color = SDL_MapRGB(screen->format, R, G, B);

    if (SDL_MUSTLOCK(screen)) {
        if (SDL_LockSurface(screen) < 0) {
            return;
        }
    }

    switch (screen->format->BytesPerPixel) {
        case 1: { // Assuming 8-bpp
            Uint8* bufp;

            bufp = (Uint8*)screen->pixels + y * screen->pitch + x;
            *bufp = color;
        }
        break;

        case 2: { // Probably 15-bpp or 16-bpp
            Uint16 *bufp;

            bufp = (Uint16*)screen->pixels + y * screen->pitch / 2 + x;
            *bufp = color;
        }
        break;

        case 3: { // Slow 24-bpp mode, usually not used
            Uint8* bufp;

            bufp = (Uint8*)screen->pixels + y * screen->pitch + x;
            *(bufp + screen->format->Rshift / 8) = R;
            *(bufp + screen->format->Gshift / 8) = G;
            *(bufp + screen->format->Bshift / 8) = B;
        }
        break;

        case 4: { // Probably 32-bpp
            Uint32* bufp;

            bufp = (Uint32*)screen->pixels + y * screen->pitch / 4 + x;
            *bufp = color;
        }
        break;
    }

    if (SDL_MUSTLOCK(screen)) {
        SDL_UnlockSurface(screen);
    }

    SDL_UpdateRect(screen, x, y, 1, 1);
}

The drawPixel is as is provided by the introduction, now the ppm file I am trying to use is called cake.ppm and its 720x540, however when I build and run this code, I get the application is not responding. I tried it on a smaller ppm file which is 426x299 and it showed a window with colors being put on the window.

  1. Why is it not working on the cake.ppm file and on others it works? Is it due to size?
  2. When I try the ppm file, the second one 426x299 or other ppm files, the colors come totally different, why is that?
  3. When I run the app, after the pixels are put, the window closes, how can I keep it?

Attempting at a file squares.ppm, here is what it should be: what it should be

But this is what I'm getting what I'm getting

genpfault
  • 51,148
  • 11
  • 85
  • 139
hakuna matata
  • 3,243
  • 13
  • 56
  • 93
  • What are values of ppm.height() and ppm.width() when your run with cake.ppm? –  Apr 07 '13 at 22:03
  • ppm.height() is 540 and ppm.width() is 720 – hakuna matata Apr 08 '13 at 04:40
  • Have you tried using a debugger to check where your app hangs, when it get's into the "not responding" state? Also: Try to draw a structure you can easily identify into your image to see how it is displayed (you could e.g. use a cross with differntly colored arms). Are the colors correct? Is the position of the structure corred? – rincewound Apr 20 '13 at 18:29
  • If you're just starting with SDL I would suggest going for SDL_image rather than drawing yourself. – Cramer Jun 21 '13 at 11:10

0 Answers0