problem with memset

hi guys,

I seem to be having a problem in SDL,yes I am still playing around with it I'm bored lol

so the problem lies mainly with memset,rather it does not give the desired output I'm expecting,what I'm expecting is all the pixels to be set to 0 or white but the window is still black

I tried testing memset out on a string to see if maybe it was calling the wrong memset (I know there is also an std::memset) but no it changes the string with no problems,

so maybe it's something I have messed up with rendering to the screen but I just can't see the problem.

thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  #include <iostream>
#include <SDL2/SDL.h>
#include <stdio.h>
#include <string.h>

using namespace std;

const int SCREENHEIGHT = 600;
const int SCREENWIDTH = 600;
SDL_Window *window;
SDL_Texture *texture;
SDL_Renderer *renderer;
SDL_Event event;
Uint32 *buffer;
char str[] = {'a','d','a','m','\0'};

void screenInit(){

    cout << str << endl;

    memset(&str[2],'x',2);  // memcopy works here

    cout << str << endl;

    if(SDL_Init(SDL_INIT_EVERYTHING) < 0){

        cout << "init error" << endl;
    }

    buffer = new Uint32[SCREENHEIGHT * SCREENWIDTH];
    window = SDL_CreateWindow("Pixel manip",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED
    ,SCREENWIDTH,SCREENHEIGHT,SDL_WINDOW_RESIZABLE);
    texture = SDL_CreateTexture(renderer,SDL_PIXELFORMAT_RGBA8888,SDL_TEXTUREACCESS_STATIC,SCREENWIDTH,SCREENHEIGHT);
    renderer = SDL_CreateRenderer(window,-1,SDL_RENDERER_PRESENTVSYNC);
}

void updateTexture(){

   memset(buffer,0,SCREENHEIGHT * SCREENWIDTH * sizeof(Uint32));
   SDL_UpdateTexture(texture,NULL,buffer,SCREENWIDTH * sizeof(Uint32));
}


void render(){


    SDL_RenderClear(renderer);
    SDL_RenderCopy(renderer,texture,NULL,NULL);
    SDL_RenderPresent(renderer);
}


int SDL_main(int argc,char* argv[])
{

    screenInit();
    bool quit = false;

    while(!quit){

        SDL_PollEvent(&event);

        if(event.type == SDL_QUIT){

            quit = true;
        }
        updateTexture();
        render();
    }
}
Isn't 0 black? Try 255 instead, although memset may not be the way to do this.
Don't forget to check your errors! SDL_GetError() is your friend. You've tried to create a texture with a renderer that hasn't been created yet.

1
2
3
4
5
6
7
8
9
  buffer = new Uint32[ SCREENHEIGHT * SCREENWIDTH ];
  window = SDL_CreateWindow("Pixel manip", SDL_WINDOWPOS_CENTERED,
                            SDL_WINDOWPOS_CENTERED, SCREENWIDTH, SCREENHEIGHT,
                            SDL_WINDOW_RESIZABLE);
  // NOTE(mbozzi): create the renderer first
  renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC);
  texture =
      SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888,
                        SDL_TEXTUREACCESS_STATIC, SCREENWIDTH, SCREENHEIGHT);

Hey tpb, no 0 is white, tried 255 which is black,

I'm befuddled I don't understand what's going wrong lol

also very good point mbozzi and that actually was the problem :o
Last edited on
No, 0,0,0 is black and 255,255,255 is white. Using memset() has the additional consequence of setting the alpha byte of your RGBA color quadruplet.
@adam, it's most likely that 0 is black and 255 is white. That's how it is everywhere else!

And memset isn't good because of the alpha byte, as Duthomhas said. You could try this:
 
fill(buffer, buffer + SCREENHEIGHT * SCREENWIDTH, 0xffffff);  // or possibly 0xffffff00 

Topic archived. No new replies allowed.