Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - ArkPhaze

Pages: [1] 2 3 ... 8
1
C - C++ / Re: [C++] making some screenshots with gdiplus
« on: January 31, 2016, 03:26:03 am »
Hello you guys. I'm working a bit on improving my C++ so I decided to mess around with screenshots. it's windows based an uses gdiplus and windows api. It gets your window with the windows api then handles the bitmap that it generates with gdiplus and saves it to a file.

The screenshot header file
Code: [Select]
#include <Windows.h>
#include <gdiplus.h>


#pragma comment(lib, "gdiplus.lib")
#pragma once

class Screenshot
{
public:
    Screenshot();
    ~Screenshot();
    void makeScreenshot(const WCHAR*);
private:
    ULONG_PTR token;
    int GetEncoderClsid(const WCHAR*, CLSID*);
};

The screenshot class

Code: [Select]
#include "Screenshot.h"

ULONG_PTR gdiToken;

Screenshot::Screenshot()
{
    Gdiplus::GdiplusStartupInput StartupInput;
    Gdiplus::GdiplusStartup(&gdiToken, &StartupInput, NULL);
}


Screenshot::~Screenshot()
{
    Gdiplus::GdiplusShutdown(gdiToken);
}

int Screenshot::GetEncoderClsid(const WCHAR* format, CLSID* pClsid) {
    UINT num = 0;
    UINT size = 0;

    Gdiplus::ImageCodecInfo* pImageCodecInfo = NULL;
    Gdiplus::GetImageEncodersSize(&num, &size);
   
    if (size == 0)
        return -1;

    pImageCodecInfo = (Gdiplus::ImageCodecInfo*)(malloc(size));

    if (pImageCodecInfo == NULL)
        return -1;

    Gdiplus::GetImageEncoders(num, size, pImageCodecInfo);

    for (UINT codec = 0; codec < num; ++codec)
    {
        if (wcscmp(pImageCodecInfo[codec].MimeType, format) == 0)
        {
            *pClsid = pImageCodecInfo[codec].Clsid;
            free(pImageCodecInfo);
            return codec;
        }
    }

    free(pImageCodecInfo);
    return -1;
}

void Screenshot::makeScreenshot(const WCHAR* filename) {
    int screenHeight = GetSystemMetrics(SM_CYSCREEN);
    int screenWidth = GetSystemMetrics(SM_CXSCREEN);

    HWND desktopWindow = GetDesktopWindow();

    HDC desktopDC = GetDC(desktopWindow);
    HDC captureDC = CreateCompatibleDC(desktopDC);

    HBITMAP captureBitmap = CreateCompatibleBitmap(desktopDC, screenWidth, screenHeight);
    SelectObject(captureDC, captureBitmap);
    BitBlt(captureDC, 0, 0, screenWidth, screenHeight, desktopDC, 0, 0, SRCCOPY | CAPTUREBLT);

    Gdiplus::Bitmap *bitmap = new Gdiplus::Bitmap(captureBitmap, NULL);
    CLSID clsId;

    int retVal = this->GetEncoderClsid(L"image/png", &clsId);
    bitmap->Save(filename, &clsId, NULL);
   
    ReleaseDC(desktopWindow, desktopDC);
    DeleteDC(captureDC);
    DeleteObject(captureBitmap);
}

That is an odd place to put #pragma once. Do you know that you have effectively made it completely redundant? I think you should look up what it is actually meant to do, before moving it to the top of your file. Why are you using malloc() and free() in C++ though? You don't really need to use GDI for this task either.

Hi buddy,
                  i just compiled your code in gcc compiler by using following command
"g++ filename.cpp  screenshot.h -o filename.exe"
That is give  undefined reference says lot .what i will do to overcome this problem


thanks  ;)

You have to compile using Visual Studio for this code as it stands.

2
C - C++ / Re: A little help?
« on: January 10, 2016, 10:03:04 am »
You don't need to use log for this:
Code: [Select]
#include <stdio.h>

int main(void)
{
  unsigned int v = 35;

  v |= v >> 1;
  v |= v >> 2;
  v |= v >> 4;
  v |= v >> 8;
  v |= v >> 16;

  printf("%u\n", -~v);
  return 0;
}

3
C - C++ / Re: Rearranging Numbers in Ascending Order
« on: December 26, 2015, 12:08:39 am »
It's good to break down the problems you have into simpler ones. Then try to solve one after another. My point is, although this problem is simple enough, you know that you have to sort an array. That's something to start with. Google and learn to sort arrays, implement it in your program, and then continue with whatever is next.

It's best if you could sit down and learn C/C++ from the beginning. You're trying to solve something without knowing the right tools and that's why you're stuck. But however, if you prefer to tackle a problem right away, break it down to specific tasks and learn how to do them separately.

That is called 'divide and conquer' in terms of CS theory. Recursion is an example of this, yet recursion isn't always considered a "good" thing and can lead to very inefficient code if the compiler can't optimize it for things like tail-call optimization among other things. Thus it can be good, but usually only as long as you understand these optimization requirements.

4
C - C++ / Re: Rearranging Numbers in Ascending Order
« on: December 16, 2015, 03:20:11 am »
^ Not exactly.

Quote
In C you should use stdio.h, iostream is a C++ library

It seemed like you were implying that he is using C and should use stdio.h, because iostream is a C++ library as far as I interpret this. Either way, there's no rulebook that says what he should do. std::cout and std::cin are slower, albeit the more C++ style approach. I know many people who write C++ code like it is C, just so that they can reap the benefits of std::vector and a few other things when they need it -- a few of the very good features of the language of C++ aside from some of the bloat.

5
C - C++ / Re: Rearranging Numbers in Ascending Order
« on: December 15, 2015, 07:48:56 am »
You're using iostream for input and stdio.h for output for some reason. In C you should use stdio.h, iostream is a C++ library.

Printing "k" is pointless because k by itself is actually a pointer to the beginning of that array. You want to print elements of it.

My hints are:
1. First, read all the elements in one loop.
2. Rearrange the array so it fits your criteria. You want ascending order so every element should be greater than all of those before it, not some temp element. Not sure what your idea was with that one too.
3. Print the array correctly.

You should really go learn the basics of the language from the beginning, and not ask people to help you with your homework. And if you do, there are plenty of other places you can do so, this one is primarily for learning.

0.o... What hints at the fact he is *using* C if it compiles fine when he includes the <iostream> header? It's obviously C++, not C. In C++ he should be using <iostream> not <stdio.h> for console I/O typically.

6
.NET Framework / Re: Just a couple of conventions ;)
« on: December 14, 2015, 12:02:45 am »
Instead of just copying content over the web why not just link to it? http://blogs.msdn.com/b/brada/archive/2005/01/26/361363.aspx

You clearly provided no credits to the original source either.

7
Assembly - Embedded / Re: [Arduino] Motion activated christmas music
« on: December 13, 2015, 11:55:27 pm »
Not bad for a little fun.

This:
Code: [Select]
playNum += 1;
if (playNum >= 2) {
  playNum = 0;
}

Can be cleanly re-written as this too:
Code: [Select]
playNum ^= 1;

8
C - C++ / Re: 'char' rather than 'int' causing double loop
« on: December 13, 2015, 05:47:45 am »
The loop happens because the first extraction for a char datatype takes the first digit of the multiple digits as input, and the second recognizes that there is data remaining in the stdin buffer, so it doesn't wait for the user to input anything and takes it automatically.

Ex: Input a char > '12'

The extraction takes '1', and when the next extraction from stdin takes place, the '2' is still left in the stream's buffer, therefore, there's no need to wait for the user to input any data because it can just take it from stdin. And because of the way scanf() and std::cin's operator>> work, it will only take data out of the stdin buffer if it can be properly represented in the datatype associated with the pointer passed to the function. If we enter a string when the function expects an int, the variable is never assigned a meaningful value, the function returns an indication of this, and the string is left in stdin.

A way to deal with that, along with a similar solution is as follows:
Code: [Select]
#include <stdio.h>
#include <stdlib.h>

#pragma GCC diagnostic ignored "-Wunused-parameter"
int main(int argc, const char *argv[])
{
  int num;
  int tries = 0;
  do
  {
    printf("Please enter a number other than %d: ", tries);

    while (scanf(" %d", &num) != 1)
    {
      while ((num = getchar()) != EOF && num != '\n');
      fputs("Invalid input, please try again: ", stderr);
    }

    /* after 10 iterations -- from a 0-based 'tries' count, the 11th
     * iteration will be 10, thus we should exit after our message */
    if (tries == 10)
    {
      fputs("Wow, you're more patient than I am, you win.", stdout);
      exit(0);
    }
  }
  while (num != tries++);
  printf("Hey! you weren't supposed to enter %d!\n", num);
  exit(0);
}


Same reason why you need a std::cin.get() after std::cin >> int_variable, or something else to get rid of the '\n' from the buffer because it's what gets appended when you confirm input with the enter key. With a string like "1234\n" as input, only the "1234" part can be represented as an int (1234), and the '\n' which remains is left in the input buffer. To avoid future conflict you should remove it somehow as soon as you can.

The way stream objects like std::cin work in C++ -- they maintain state flags that you should be checking for indication of success or failure whenever you deal with read/extraction operations like with the overloaded >> operator.

9
C - C++ / Re: Check if a string is inside a string
« on: December 13, 2015, 05:31:42 am »
You have the misunderstanding of "re-inventing the wheel" vs. "innovation" which is mistake #1. If tablets and such were the results of "re-inventing the wheel" they would not contain the same core code as most of the desktop counterparts. Additionally, most innovative ideas from my experience were the result of "_expanding_ the wheel" not "re-inventing" it unless there is a fundamental issue with the pre-existing "wheel" so to speak.

10
C - C++ / Re: What to use?
« on: December 13, 2015, 05:12:14 am »
@rogue.hackz & ArkPhase - I've been programming for 20 years, I started on the C88, with vi, on a console based bare bones 0.3 Red Hat. I was taught embedded systems design by a 30+ year senior embedded programmer for NASA. I see absolutely no point in using any of the flashy bullshit most n00bs use these days. The truth is, N00Bs have to use those plugins, and all those "extras", cause they are too busy being too new, to keep track of simple bullshit... If you are incapable of keeping track of your program's execution without needing "documentation", you're a n00b, if you can not deduce your mistakes without needing some "auto error finder", you're a n00b. More importantly, if you can not code in a manner, that is self-secured, and hard to exploit; you are a n00b. Period. So, in other words, you are kids... I am a kid.

I have no idea who 'ArkPhase' is but the way you type makes me think you're 13 in all honesty. Btw, unless everything is always standardized, optimized code is usually fairly cryptic under most circumstances so if you want to waste time having to re-analyze your code every time you need to go back to it? Whatever the reason is that you need to review old code, good luck remembering what that code was meant to do as well. I've had scenarios where it's been about a year since I had last seen parts of my code in some projects, and it all seemed like a blur in terms of me even writing the code. The other benefit of documentation for code is noticeable when you work in a team environment -- other people have to be able to read and understand your code as well; save them the time and effort of wasting any more time than necessary having to read through your actual code to interpret it.

Nobody said noobs have to use anything, noobs generally won't start out with git or use memory profilers on day 1 anyways so your logic is flawed. What most people here are talking about is productivity and accessories that help with debugging code -- if it didn't matter, tools like valgrind, cachegrind, and many others would not exist because those developers wouldn't have had the motivation to even dream about creating a program like it. You can keep your delusional opinions to yourself from now on as you clearly have zero experience in the real world, or hardly any of it. You cannot fool us...

And I know Xires is a member here that I can respect without even bothering to know about any of his credentials, along with a few others. Why? Because they don't feel the need to boast about their credentials; any good programmer doesn't have to tell other programmers about what they have done or what they can do -- it shows in the remarks they make and the code they write. I have yet to see the same from you, and until that point in time I will respectfully dismiss those credentials and cherry pick the relevant parts of your post describing your ideas which are really just your own opinions, of which they shall remain.

11
C - C++ / Re: What to use?
« on: October 23, 2015, 02:18:19 am »
You have a long way to go, kid. I use vim, without any plugins to code in the language I want. If you can't code without autocomplete, you seriously need to work hard on your skills.
I can understand coding Java with autocomplete as you seriously need to type all the class.function name but not in C, C++ -_-

You'd rather waste your time writing everything out rather than actually working with the logic of the code itself? Development with C/C++ is already slow as it is, *tools* like auto-complete improve workflow, and I use Vim myself. It has nothing to do with not being able to code without it, there's just no point in not using something that can help you write your code faster.



You do realize that the whole point in even using vim is to make text manipulation and editing faster and more efficient? It's the whole reason why movements exist in a way that allows you to not move your hands *far*. If you aren't using plugins, you're really not taking advantage of the full feature-set of the editor itself IMO.

If it was up to the programmer to do everything, notepad would be the only program people use, but that's not the case -- you have much to learn.

Quote
I can understand coding Java with autocomplete as you seriously need to type all the class.function name but not in C, C++ -_-

What? Namespaces and static member functions, etc... Don't exist in C++? Have you ever used the boost library? You'd see how verbose some code can be if you ever have...

12

Code: [Select]
printf("%s",( 0xff | 0xf1 == 110 | 011 == ((int)true) == (int)!false || 0 | 0 ) ? "Yeah that was a good feedback ..." : "I made a mistake");


O.o... What is the point of this?

13
Just for fun :)

Code: [Select]
```
 ~/fun  cat test.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h>


char** split(char *s,const char *delim) {
        int delims = 0; char *p = strtok(s,delim), **res = NULL;
    while(p) {
       res = realloc(res,sizeof(char*) * ++delims); // allocating memory
           if ( res == NULL ) exit(-1); // there is no more memory , allocation is failed
           res[delims-1] = p;
       p = strtok(NULL,delim);
    }
        // adding last element because of the null 
        res = realloc(res,sizeof(char*) * ++delims);
        res[delims-1] = 0;
        return res;
   
}

int main () {
  char buf[] ="it is just a simple example for fun";
  char** data = split(buf," ");
  for(int i=0; data[i]; i++)
     printf("%d = %s\n",i, data[i] );
  free(data);
}

```

That realloc() call for each token found is really an ugly idea though..

Also your last couple lines could be this instead:
Code: [Select]
res = realloc(res, sizeof(char *) * delims + 1;
res[delims] = 0;

No need to increment and store the value of delims + 1 in delims if you are going to subtract from it afterwards.

14
C - C++ / Re: Check if a string is inside a string
« on: July 27, 2015, 07:09:19 am »
Why not just use the strstr() function?

Otherwise, I'd have something like this:
Code: [Select]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *xstrstr(char *src, const char *find)
{
  size_t i = 0;
  size_t srclen = strlen(find);
  char *p = NULL;
  for (p = src; *p; ++p)
  {
    for (i = 0; i < srclen; ++i)
    {
      if (!*(p + i)
          || *(p + i) != *(find + i))
      {
        break;
      }
    }
    if (i == srclen)
      return p;
  }
  return NULL;
}

int main()
{
  char src[] = "this is a simple test";
  const char find[] = "simple";

  char *p = xstrstr(src, find);
  if (!p)
  {
    puts("Not found...\n");
    exit(0);
  }

  printf("Found @ src[%d]: %s\n", p - src, p);
  return 0;
}

Obvious optimizations still can be implemented, for example, here:
Code: [Select]
for (p = src; *p; ++p)

15
C - C++ / Re: Concatenate file (single byte xor)
« on: June 05, 2015, 03:59:02 am »
Well, as much as I hate to admit it ;) , you're correct about sizeof(char), it's definitely equal to one according to the standard.*  I see what you mean about the chkret macro now, not freeing what it should, good catch.

*I took some time to do a little more research into the subject and even if a "char" is 32 bits, sizeof(char) will always return 1.  You can also find the number of bits contained in a char by looking at CHAR_BITS in <limits.h>. Source: (ctrl+f) for section 6.5.3.4

I know... And it's not CHAR_BITS, it's CHAR_BIT. Although, look at sizeof('x'), which is 4 even if CHAR_BIT is 8 too. There's a big difference between the way char literals are interpreted and the way the char datatype by the standard is supposed to behave and work, but there are good reasons for it too, and it doesn't really affect the programmer in any way as long as they know what they are doing..

Pages: [1] 2 3 ... 8