SDL_Rect rendering the full size of the screen

hey guys,

I feel this question is too basic to put in anything other than the beginners section,

anyway I'm trying to draw a button(SDL_Rect) to the window,but the only problem is the SDL_Rect is filling the whole window,my constraints do not specify for the full window to be used


any idea whats going on?

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
71
72
73
74
75
76
77
78
79
  #include <iostream>
#include <SDL2/SDL.h>

using namespace std;


typedef struct {
    SDL_Rect draw_rect;    // dimensions of button
    struct {
        Uint8 r, g, b, a;
    } colour;

    bool pressed;
} button_t;


SDL_Window* window;
SDL_Renderer* renderer;
SDL_Event event;
const int HEIGHT = 500;
const int WIDTH = 500;


bool initScreen(){

    if(SDL_Init(SDL_INIT_EVERYTHING) < 0){

        return false;
    }
    window = SDL_CreateWindow("Button",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,WIDTH,HEIGHT,SDL_WINDOW_RESIZABLE);
    renderer = SDL_CreateRenderer(window,-1,0);
    SDL_SetRenderDrawColor(renderer,255,255,255,255);

}

static bool button(SDL_Renderer *r, button_t *btn) {
    // draw button
    SDL_SetRenderDrawColor(r, btn->colour.r, btn->colour.g, btn->colour.b, btn->colour.a);
    SDL_RenderFillRect(r, &btn->draw_rect);
    // if button press detected - reset it so it wouldn't trigger twice
    if(btn->pressed) {
        btn->pressed = false;
        return true;
    }
    return false;
}


void renderScreen(){

   SDL_RenderClear(renderer);
   SDL_RenderPresent(renderer);

}


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

    initScreen();

    button_t start_button;
    start_button.colour.r = 0;
    start_button.colour.g = 0;
    start_button.colour.b = 0;
    start_button.draw_rect.h = 50;
    start_button.draw_rect.w = 50;
    start_button.draw_rect.x = 50;
    start_button.draw_rect.y = 50;


    while(true){

        SDL_PollEvent(&event);
        button(renderer,&start_button);
        renderScreen();
    }
}
...
Last edited on
Thanks Tpb,

but I beg to differ,I have seen plenty of your posts on here helping people and to be honest I doubt you have much more knowledge than me,I will admit you are no doubt a better programmer than me from what I have gauged but not by much,if you declare me as beginner I would probably declare you as beginner++,

but I digress,I'm not a games programmer I actually have very little interest in game programming,I have used SDL a very minimal amount of times,I have followed a few tutorials here and there I'm just pretty much messing around and following a few tutorials to become a better programmer as I would,obviously this isn't a fully functioning program I'm just getting to grips with some basic concepts and uses of the SDL library,

and if you have ever programmed using the SDL library you should know that you do indeed declare the main function as SDL_main with command line arguments,I could be wrong but I'm almost certain I'm not. If I am please correct me but my compiler throws up a lot of error messages without main declared as SDL_main,

you are pointing out very obvious things I know you need to break out of the loop(have a bool) to quit if event.type equals the number for SDL_QUIT,but I'm only doing this for testing purposes,I also know I'm not freeing/deallocating my memory again it's not a fully
functioning program.

I could be doing a lot of things better and by the book,I should probably not be using globals,I should probably use oop instead of the C style functional programming approach I have taken here,I probably should free my resources.


Again I'm not upset at you,as mentioned you are a better programmer than me and I will admit that hands down I mean it's not a competition it's just insulting to judge my skills and knowledge from just asking a simple question,I mean I have seen your posts on here and you are not at the elite level yourself(but no doubt better than me),I have a long way to go no doubt and to be honest I will admit I am not a great programmer actually quite a poor one, but to call me a beginner after 2-3 years experience is kind of insulting seeing as you probably don't have much more experience yourself,and again I've gone off on a tangent, so now that I got that off my chest now to the problem.


It's not a good idea to "poll" for events if "waiting" is good enough (SDL_WaitEvent). With polling your program constantly uses about 10% of my CPU when it's doing absolutely nothing. With waiting it uses about 0.1%
never knew that :o

sorry to go off on a rant I just found that a little disrespectful but I appreciate the help :)

Last edited on
No problem. I'll never bother you again.
As I said it's no problem at all,just felt that was kind of a slap in the face,considering I've only started learning the SDL library recently and if I was to use them practices in real production code I should be shot lol, again I was just wondering why my object was not appearing on the screen and testing out SDL_Rects it wasn't a fully fledged program.

also here's why SDL_main is required - https://stackoverflow.com/questions/11976084/why-sdl-defines-main-macro
Last edited on
I just found that a little disrespectful

How can I possibly disrespect you when there is absolutely nothing to respect? Your code is that of the rawest of raw beginners. It's repetitive thoughtless and boring. You have essentially no knowledge whatsoever of C++ and hardly any of C either. You are totally delusional if you think we are anywhere near the same level. You have mental problems. See a doctor.
lol like I have said you are a better programmer no doubt,but honestly your skills are not that far ahead,you answer very basic questions on here and when more advanced questions are asked you are no where to be seen, you must have an inflated image of yourself because honestly you aren't as good as you believe(but keep telling yourself that by all means,what ever helps you sleep at night) no need to be so butt hurt. yes you show good practices but that's about it as far as coding skills not much there. You are great at helping beginners that's about it,I don't help people on this forum yet(hopefully soon) because I am not at that level and to be honest neither are you apart from answering the most fundamental of topics.
Last edited on
Topic archived. No new replies allowed.